Make formatter<T> override ostream<< for templates (#952)
This commit is contained in:
@@ -2144,6 +2144,10 @@ struct test_context {
|
||||
typedef char char_type;
|
||||
typedef fmt::basic_format_arg<test_context> format_arg;
|
||||
|
||||
template <typename T> struct formatter_type {
|
||||
typedef fmt::formatter<T, char_type> type;
|
||||
};
|
||||
|
||||
FMT_CONSTEXPR fmt::basic_format_arg<test_context> next_arg() {
|
||||
return fmt::internal::make_arg<test_context>(11);
|
||||
}
|
||||
|
||||
@@ -178,6 +178,29 @@ template <typename Output> Output& operator<<(Output& out, ABC) {
|
||||
}
|
||||
} // namespace fmt_test
|
||||
|
||||
template <typename T>
|
||||
struct TestTemplate {};
|
||||
|
||||
template <typename T>
|
||||
std::ostream& operator<<(std::ostream& os, TestTemplate<T>) {
|
||||
return os << 1;
|
||||
}
|
||||
|
||||
namespace fmt {
|
||||
template <typename T>
|
||||
struct formatter<TestTemplate<T>> : formatter<int> {
|
||||
template <typename FormatContext>
|
||||
typename FormatContext::iterator format(TestTemplate<T>, FormatContext& ctx) {
|
||||
return formatter<int>::format(2, ctx);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 407
|
||||
TEST(OStreamTest, Template) {
|
||||
EXPECT_EQ("2", fmt::format("{}", TestTemplate<int>()));
|
||||
}
|
||||
|
||||
TEST(FormatTest, FormatToN) {
|
||||
char buffer[4];
|
||||
buffer[3] = 'x';
|
||||
@@ -190,6 +213,7 @@ TEST(FormatTest, FormatToN) {
|
||||
EXPECT_EQ(buffer + 3, result.out);
|
||||
EXPECT_EQ("xABx", fmt::string_view(buffer, 4));
|
||||
}
|
||||
#endif
|
||||
|
||||
#if FMT_USE_USER_DEFINED_LITERALS
|
||||
TEST(FormatTest, UDL) {
|
||||
|
||||
Reference in New Issue
Block a user