Skip to content

Commit

Permalink
Don't use const char* overload of operator<< (fmtlib#1309)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Sep 23, 2019
1 parent 758446c commit f299010
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 5 additions & 5 deletions include/fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ template <class Char> class formatbuf : public std::basic_streambuf<Char> {

template <typename Char> struct test_stream : std::basic_ostream<Char> {
private:
struct null;
// Hide all operator<< from std::basic_ostream<Char>.
void operator<<(null);
void_t<> operator<<(null<>);
void_t<> operator<<(const Char*);
};

// Checks if T has a user-defined operator<< (e.g. not a member of
// std::ostream).
template <typename T, typename Char> class is_streamable {
private:
template <typename U>
static decltype((void)(std::declval<test_stream<Char>&>()
<< std::declval<U>()),
std::true_type())
static bool_constant<!std::is_same<decltype(std::declval<test_stream<Char>&>()
<< std::declval<U>()),
void_t<>>::value>
test(int);

template <typename> static std::false_type test(...);
Expand Down
12 changes: 12 additions & 0 deletions test/ostream-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,15 @@ TEST(FormatTest, UDL) {
EXPECT_EQ("{}"_format("test"), "test");
}
#endif

template <typename T>
struct convertible {
T value;
explicit convertible(const T& val) : value(val) {}
operator T() const { return value; }
};

TEST(OStreamTest, ConvertibleToCString) {
EXPECT_EQ("x", fmt::format("{}", convertible<char>('x')));
EXPECT_EQ("foo", fmt::format("{}", convertible<const char*>("foo")));
}

0 comments on commit f299010

Please sign in to comment.