From 5a866fe852c698163d0e95f0f48a64528eca45f6 Mon Sep 17 00:00:00 2001 From: Zhanwei Wang Date: Mon, 14 Aug 2023 23:34:31 +0800 Subject: [PATCH] Add formatter for std::atomic (#3574) --- include/fmt/std.h | 14 +++++++++++++- test/std-test.cc | 8 ++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/include/fmt/std.h b/include/fmt/std.h index b0e78e10..5cfe1d87 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -8,6 +8,7 @@ #ifndef FMT_STD_H_ #define FMT_STD_H_ +#include #include #include #include @@ -435,6 +436,17 @@ struct formatter +struct formatter, Char, + enable_if_t::value>> + : formatter { + template + auto format(const std::atomic& v, FormatContext& ctx) const + -> decltype(ctx.out()) { + return formatter::format(v.load(), ctx); + } +}; +FMT_END_NAMESPACE #endif // FMT_STD_H_ diff --git a/test/std-test.cc b/test/std-test.cc index 17b4792c..d0617a73 100644 --- a/test/std-test.cc +++ b/test/std-test.cc @@ -236,3 +236,11 @@ TEST(std_test, format_const_bit_reference) { const std::vector v = {true, false}; EXPECT_EQ(fmt::format("{} {}", v[0], v[1]), "true false"); } + +TEST(std_test, format_atomic) { + std::atomic b(false); + EXPECT_EQ(fmt::format("{}", b), "false"); + + const std::atomic cb(true); + EXPECT_EQ(fmt::format("{}", cb), "true"); +}