Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,25 @@ struct formatter<
}
};

template <> struct formatter<std::exception_ptr> : formatter<std::exception> {
template <typename FormatContext>
auto format(const std::exception_ptr& ex_ptr, FormatContext& ctx) const
-> decltype(ctx.out()) {
if (!ex_ptr) {
return detail::write(ctx.out(), string_view("nullptr"));
}

try {
std::rethrow_exception(ex_ptr);
} catch (const std::exception& e) {
// Reuses the base formatter<std::exception>::format behavior perfectly
return formatter<std::exception>::format(e, ctx);
} catch (...) {
return detail::write(ctx.out(), string_view("unknown exception"));
}
}
};

template <int N, typename Char>
struct formatter<detail::bitint<N>, Char> : formatter<long long, Char> {
static_assert(N <= 64, "unsupported _BitInt");
Expand Down
20 changes: 20 additions & 0 deletions test/std-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,26 @@ TEST(std_test, exception) {
#endif
}

TEST(std_test, exception_ptr) {
std::exception_ptr p1 = nullptr;
std::exception_ptr p2;

try {
using namespace my_ns1::my_ns2;
throw my_exception("My Exception");
} catch (...) {
p2 = std::current_exception();
}

EXPECT_EQ(fmt::format("{}", p1), "nullptr");
EXPECT_EQ(fmt::format("{}", p2), "My Exception");

#if FMT_USE_RTTI
EXPECT_EQ(fmt::format("{:t}", p2),
"my_ns1::my_ns2::my_exception: My Exception");
#endif
}

#if FMT_USE_RTTI
TEST(std_test, type_info) {
EXPECT_EQ(fmt::format("{}", typeid(std::runtime_error)),
Expand Down