diff --git a/include/fmt/std.h b/include/fmt/std.h index 7cd7ef3af3584..176cf083e62e0 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -174,13 +174,11 @@ FMT_BEGIN_NAMESPACE template struct formatter>> - : formatter> { + : formatter { template auto format(const std::exception& ex, FormatContext& ctx) const -> typename FormatContext::iterator { - auto out = ctx.out(); - detail::write(out, ex.what()); - return out; + return fmt::formatter::format(ex.what(), ctx); } }; FMT_END_NAMESPACE diff --git a/test/std-test.cc b/test/std-test.cc index 350548e81541f..4d0e5b5188786 100644 --- a/test/std-test.cc +++ b/test/std-test.cc @@ -84,6 +84,7 @@ TEST(std_test, exception) { std::ignore = vec.at(42); } catch (const std::exception& ex) { EXPECT_EQ(fmt::format("{}", ex), "invalid vector subscript"); + EXPECT_EQ(fmt::format("{:?}", ex), "\"invalid vector subscript\""); } try { @@ -91,5 +92,6 @@ TEST(std_test, exception) { std::ignore = vec.at(42); } catch (const std::out_of_range& ex) { EXPECT_EQ(fmt::format("{}", ex), "invalid vector subscript"); + EXPECT_EQ(fmt::format("{:?}", ex), "\"invalid vector subscript\""); } }