From 5f37d3b99808349fb9140cb65b75407e8657451a Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Wed, 18 Sep 2024 23:19:56 +0500 Subject: [PATCH] Use fmt::formatter specialization for std::reference_wrapper to avoid undefined behavior Signed-off-by: Vladislav Shchapov --- include/fmt/std.h | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/include/fmt/std.h b/include/fmt/std.h index be1cc56d7d1a..0370f781033f 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -692,15 +692,17 @@ template struct formatter, Char> { } }; -FMT_END_NAMESPACE - -namespace std { - -template -constexpr auto format_as(std::reference_wrapper ref) -> T& { - return ref.get(); -} - -} // namespace std +FMT_EXPORT +template +struct formatter, Char, + enable_if_t, Char>::value>> + : formatter, Char> { + template + auto format(std::reference_wrapper ref, FormatContext& ctx) const + -> decltype(ctx.out()) { + return formatter, Char>::format(ref.get(), ctx); + } +}; +FMT_END_NAMESPACE #endif // FMT_STD_H_