Skip to content

Commit

Permalink
Workaround brain-damaged conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jun 26, 2023
1 parent a331dbf commit 606f85f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions test/core-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit 606f85f

Please sign in to comment.