Workaround brain-damaged conversions

This commit is contained in:
Victor Zverovich 2023-06-26 15:46:12 -07:00
parent a331dbfb65
commit 606f85f8b2
2 changed files with 28 additions and 1 deletions

View File

@ -286,8 +286,10 @@ struct monostate {
#endif
// 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
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);
}
#endif

View File

@ -831,3 +831,28 @@ TEST(core_test, has_const_formatter) {
TEST(core_test, format_nonconst) {
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");
}