Skip to content

Commit

Permalink
Use fmt::formatter specialization for std::reference_wrapper to avoid…
Browse files Browse the repository at this point in the history
… undefined behavior

Signed-off-by: Vladislav Shchapov <[email protected]>
  • Loading branch information
phprus committed Sep 19, 2024
1 parent ed8f8be commit 5f37d3b
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -692,15 +692,17 @@ template <typename T, typename Char> struct formatter<std::complex<T>, Char> {
}
};

FMT_END_NAMESPACE

namespace std {

template <typename T>
constexpr auto format_as(std::reference_wrapper<T> ref) -> T& {
return ref.get();
}

} // namespace std
FMT_EXPORT
template <typename T, typename Char>
struct formatter<std::reference_wrapper<T>, Char,
enable_if_t<is_formattable<remove_cvref_t<T>, Char>::value>>
: formatter<remove_cvref_t<T>, Char> {
template <typename FormatContext>
auto format(std::reference_wrapper<T> ref, FormatContext& ctx) const
-> decltype(ctx.out()) {
return formatter<remove_cvref_t<T>, Char>::format(ref.get(), ctx);
}
};

FMT_END_NAMESPACE
#endif // FMT_STD_H_

0 comments on commit 5f37d3b

Please sign in to comment.