From 1f87b1c58d2d466b283f282211b732a4e3987659 Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Thu, 19 Sep 2024 22:34:33 +0500 Subject: [PATCH] Use fmt::formatter specialization for std::reference_wrapper to avoid undefined behavior (#4164) 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 be1cc56d..0370f781 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -692,15 +692,17 @@ template struct formatter, Char> { } }; +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 - -namespace std { - -template -constexpr auto format_as(std::reference_wrapper ref) -> T& { - return ref.get(); -} - -} // namespace std - #endif // FMT_STD_H_