Skip to content

Commit

Permalink
Disable integral operator<< (#1316)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Sep 24, 2019
1 parent 20fdb88 commit ccc8f5d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 4 additions & 0 deletions include/fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ template <typename Char> struct test_stream : std::basic_ostream<Char> {
// Hide all operator<< from std::basic_ostream<Char>.
void_t<> operator<<(null<>);
void_t<> operator<<(const Char*);

template <typename T, FMT_ENABLE_IF(std::is_convertible<T, int>::value &&
!std::is_enum<T>::value)>
void_t<> operator<<(T);
};

// Checks if T has a user-defined operator<< (e.g. not a member of
Expand Down
5 changes: 3 additions & 2 deletions test/ostream-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ struct convertible {
operator T() const { return value; }
};

TEST(OStreamTest, ConvertibleToCString) {
EXPECT_EQ("x", fmt::format("{}", convertible<char>('x')));
TEST(OStreamTest, DisableBuiltinOStreamOperators) {
EXPECT_EQ("42", fmt::format("{:d}", convertible<unsigned short>(42)));
EXPECT_EQ(L"42", fmt::format(L"{:d}", convertible<unsigned short>(42)));
EXPECT_EQ("foo", fmt::format("{}", convertible<const char*>("foo")));
}

0 comments on commit ccc8f5d

Please sign in to comment.