Skip to content

Commit

Permalink
Add detection of wostream operator<< (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Feb 17, 2018
1 parent 1efc15c commit 91721ca
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
12 changes: 6 additions & 6 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,16 +413,16 @@ FMT_CONSTEXPR bool is_arithmetic(type t) {
return t > internal::none_type && t <= internal::last_numeric_type;
}

template <typename T, bool ENABLE = true>
template <typename T, typename Char, bool ENABLE = true>
struct convert_to_int {
enum {
value = !std::is_arithmetic<T>::value && std::is_convertible<T, int>::value
};
};

#define FMT_DISABLE_CONVERSION_TO_INT(Type) \
template <> \
struct convert_to_int<Type> { enum { value = 0 }; }
template <typename Char> \
struct convert_to_int<Type, Char> { enum { value = 0 }; }

// Silence warnings about convering float to int.
FMT_DISABLE_CONVERSION_TO_INT(float);
Expand Down Expand Up @@ -589,13 +589,13 @@ void make_value(const T *p) {

template <typename C, typename T>
inline typename std::enable_if<
convert_to_int<T>::value && std::is_enum<T>::value,
convert_to_int<T, typename C::char_type>::value && std::is_enum<T>::value,
typed_value<C, int_type>>::type
make_value(const T &val) { return static_cast<int>(val); }

template <typename C, typename T>
inline typename std::enable_if<
!convert_to_int<T>::value, typed_value<C, custom_type>>::type
inline typename std::enable_if<!convert_to_int<
T, typename C::char_type>::value, typed_value<C, custom_type>>::type
make_value(const T &val) { return val; }

template <typename C, typename T>
Expand Down
11 changes: 6 additions & 5 deletions include/fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,22 @@ class FormatBuf : public std::basic_streambuf<Char> {
}
};

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

// Disable conversion to int if T has an overloaded operator<< which is a free
// function (not a member of std::ostream).
template <typename T>
class convert_to_int<T, true> {
template <typename T, typename Char>
class convert_to_int<T, Char, true> {
private:
template <typename U>
static decltype(
std::declval<test_stream&>() << std::declval<U>(), std::true_type())
std::declval<test_stream<Char>&>() << std::declval<U>(), std::true_type())
test(int);

template <typename>
Expand Down
2 changes: 1 addition & 1 deletion test/ostream-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ std::ostream &operator<<(std::ostream &os, TestEnum) {
enum TestEnum2 {A};

TEST(OStreamTest, Enum) {
EXPECT_FALSE(fmt::internal::convert_to_int<TestEnum>::value);
EXPECT_FALSE((fmt::internal::convert_to_int<TestEnum, char>::value));
EXPECT_EQ("TestEnum", fmt::format("{}", TestEnum()));
EXPECT_EQ("0", fmt::format("{}", A));
}
Expand Down
8 changes: 4 additions & 4 deletions test/util-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -843,15 +843,15 @@ TEST(UtilTest, ReportWindowsError) {
enum TestEnum2 {};

TEST(UtilTest, ConvertToInt) {
EXPECT_FALSE(fmt::internal::convert_to_int<char>::value);
EXPECT_FALSE(fmt::internal::convert_to_int<const char *>::value);
EXPECT_TRUE(fmt::internal::convert_to_int<TestEnum2>::value);
EXPECT_FALSE((fmt::internal::convert_to_int<char, char>::value));
EXPECT_FALSE((fmt::internal::convert_to_int<const char *, char>::value));
EXPECT_TRUE((fmt::internal::convert_to_int<TestEnum2, char>::value));
}

#if FMT_USE_ENUM_BASE
enum TestEnum : char {TestValue};
TEST(UtilTest, IsEnumConvertibleToInt) {
EXPECT_TRUE(fmt::internal::convert_to_int<TestEnum>::value);
EXPECT_TRUE((fmt::internal::convert_to_int<TestEnum, char>::value));
}
#endif

Expand Down

0 comments on commit 91721ca

Please sign in to comment.