diff --git a/include/fmt/os.h b/include/fmt/os.h index d82be1125a4b..8e697ec4e6fa 100644 --- a/include/fmt/os.h +++ b/include/fmt/os.h @@ -120,24 +120,6 @@ template class basic_cstring_view { using cstring_view = basic_cstring_view; using wcstring_view = basic_cstring_view; -template struct formatter { - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - return ctx.begin(); - } - - template - FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const - -> decltype(ctx.out()) { - auto out = ctx.out(); - out = detail::write_bytes(out, ec.category().name(), - basic_format_specs()); - out = detail::write(out, Char(':')); - out = detail::write(out, ec.value()); - return out; - } -}; - #ifdef _WIN32 FMT_API const std::error_category& system_category() noexcept; diff --git a/include/fmt/std.h b/include/fmt/std.h index 2896e82a0c12..ec7abaa99f05 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -180,6 +180,25 @@ FMT_END_NAMESPACE #endif // __cpp_lib_variant FMT_BEGIN_NAMESPACE + +template struct formatter { + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + return ctx.begin(); + } + + template + FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const + -> decltype(ctx.out()) { + auto out = ctx.out(); + out = detail::write_bytes(out, ec.category().name(), + basic_format_specs()); + out = detail::write(out, Char(':')); + out = detail::write(out, ec.value()); + return out; + } +}; + template struct formatter< T, Char, diff --git a/test/os-test.cc b/test/os-test.cc index a1745c8c89ee..d93c553dc6b9 100644 --- a/test/os-test.cc +++ b/test/os-test.cc @@ -64,18 +64,6 @@ TEST(util_test, utf16_to_utf8_convert) { u.convert(wstring_view(L"foo", INT_MAX + 1u))); } -TEST(os_test, format_std_error_code) { - EXPECT_EQ("generic:42", - fmt::format(FMT_STRING("{0}"), - std::error_code(42, std::generic_category()))); - EXPECT_EQ("system:42", - fmt::format(FMT_STRING("{0}"), - std::error_code(42, fmt::system_category()))); - EXPECT_EQ("system:-42", - fmt::format(FMT_STRING("{0}"), - std::error_code(-42, fmt::system_category()))); -} - TEST(os_test, format_windows_error) { LPWSTR message = nullptr; auto result = FormatMessageW( diff --git a/test/std-test.cc b/test/std-test.cc index f700a510f74b..4411d3d1cd39 100644 --- a/test/std-test.cc +++ b/test/std-test.cc @@ -10,6 +10,7 @@ #include #include +#include "fmt/os.h" // fmt::system_category #include "fmt/ranges.h" #include "gtest-extra.h" // StartsWith @@ -83,6 +84,18 @@ TEST(std_test, variant) { #endif } +TEST(std_test, error_code) { + EXPECT_EQ("generic:42", + fmt::format(FMT_STRING("{0}"), + std::error_code(42, std::generic_category()))); + EXPECT_EQ("system:42", + fmt::format(FMT_STRING("{0}"), + std::error_code(42, fmt::system_category()))); + EXPECT_EQ("system:-42", + fmt::format(FMT_STRING("{0}"), + std::error_code(-42, fmt::system_category()))); +} + template void exception_test() { try { throw std::runtime_error("Test Exception");