mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
Workaround brain-damaged conversions
This commit is contained in:
@ -286,8 +286,10 @@ struct monostate {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// This is defined in core.h instead of format.h to avoid injecting in std.
|
// This is defined in core.h instead of format.h to avoid injecting in std.
|
||||||
|
// It is a template to avoid undesirable implicit conversions to std::byte.
|
||||||
#ifdef __cpp_lib_byte
|
#ifdef __cpp_lib_byte
|
||||||
inline auto format_as(std::byte b) -> unsigned char {
|
template <typename T, FMT_ENABLE_IF(std::is_same<T, std::byte>::value)>
|
||||||
|
inline auto format_as(T b) -> unsigned char {
|
||||||
return static_cast<unsigned char>(b);
|
return static_cast<unsigned char>(b);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -831,3 +831,28 @@ TEST(core_test, has_const_formatter) {
|
|||||||
TEST(core_test, format_nonconst) {
|
TEST(core_test, format_nonconst) {
|
||||||
EXPECT_EQ(fmt::format("{}", nonconst_formattable()), "test");
|
EXPECT_EQ(fmt::format("{}", nonconst_formattable()), "test");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct its_a_trap {
|
||||||
|
template <typename T> operator T() const {
|
||||||
|
auto v = T();
|
||||||
|
v.x = 42;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
FMT_BEGIN_NAMESPACE
|
||||||
|
template <> struct formatter<its_a_trap> {
|
||||||
|
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
|
||||||
|
return ctx.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto format(its_a_trap, format_context& ctx) const -> decltype(ctx.out()) {
|
||||||
|
auto s = string_view("42");
|
||||||
|
return std::copy(s.begin(), s.end(), ctx.out());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
FMT_END_NAMESPACE
|
||||||
|
|
||||||
|
TEST(core_test, trappy_conversion) {
|
||||||
|
EXPECT_EQ(fmt::format("{}", its_a_trap()), "42");
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user