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"); +}