mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-21 18:20:04 +00:00
Improve compile-time formatting (#4127)
This commit is contained in:
parent
1a79bbfa83
commit
c59ee969f3
@ -1003,7 +1003,7 @@ template <typename Char, size_t N> struct fixed_string {
|
||||
}
|
||||
Char data[N] = {};
|
||||
};
|
||||
#endif
|
||||
#endif // FMT_USE_NONTYPE_TEMPLATE_ARGS
|
||||
|
||||
// Converts a compile-time string to basic_string_view.
|
||||
template <typename Char, size_t N>
|
||||
@ -3912,19 +3912,21 @@ template <typename T, typename Char>
|
||||
struct formatter<T, Char, enable_if_t<detail::has_format_as<T>::value>>
|
||||
: formatter<detail::format_as_t<T>, Char> {
|
||||
template <typename FormatContext>
|
||||
auto format(const T& value, FormatContext& ctx) const -> decltype(ctx.out()) {
|
||||
FMT_CONSTEXPR auto format(const T& value, FormatContext& ctx) const
|
||||
-> decltype(ctx.out()) {
|
||||
auto&& val = format_as(value); // Make an lvalue reference for format.
|
||||
return formatter<detail::format_as_t<T>, Char>::format(val, ctx);
|
||||
}
|
||||
};
|
||||
|
||||
#define FMT_FORMAT_AS(Type, Base) \
|
||||
template <typename Char> \
|
||||
struct formatter<Type, Char> : formatter<Base, Char> { \
|
||||
template <typename FormatContext> \
|
||||
auto format(Type value, FormatContext& ctx) const -> decltype(ctx.out()) { \
|
||||
return formatter<Base, Char>::format(value, ctx); \
|
||||
} \
|
||||
#define FMT_FORMAT_AS(Type, Base) \
|
||||
template <typename Char> \
|
||||
struct formatter<Type, Char> : formatter<Base, Char> { \
|
||||
template <typename FormatContext> \
|
||||
FMT_CONSTEXPR auto format(Type value, FormatContext& ctx) const \
|
||||
-> decltype(ctx.out()) { \
|
||||
return formatter<Base, Char>::format(value, ctx); \
|
||||
} \
|
||||
}
|
||||
|
||||
FMT_FORMAT_AS(signed char, int);
|
||||
|
@ -334,6 +334,7 @@ TEST(compile_time_formatting_test, integer) {
|
||||
EXPECT_EQ("0X4A", test_format<5>(FMT_COMPILE("{:#X}"), 0x4a));
|
||||
|
||||
EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42));
|
||||
EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42l));
|
||||
EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42ll));
|
||||
EXPECT_EQ(" 42", test_format<6>(FMT_COMPILE("{:5}"), 42ull));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user