Skip to content

Commit

Permalink
format std::reference_wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
yfeldblum committed Sep 16, 2024
1 parent 2a2f73f commit 8fac8c2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# include <complex>
# include <cstdlib>
# include <exception>
# include <functional>
# include <memory>
# include <thread>
# include <type_traits>
Expand Down Expand Up @@ -700,3 +701,12 @@ template <typename T, typename Char> struct formatter<std::complex<T>, Char> {

FMT_END_NAMESPACE
#endif // FMT_STD_H_

namespace std {

template <typename T>
constexpr auto format_as(std::reference_wrapper<T> ref) -> T& {
return ref.get();
}

} // namespace std
5 changes: 5 additions & 0 deletions test/std-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,8 @@ TEST(std_test, format_shared_ptr) {
EXPECT_EQ(fmt::format("{}", fmt::ptr(sp.get())),
fmt::format("{}", fmt::ptr(sp)));
}

TEST(std_test, format_reference_wrapper) {
int num = 35;
EXPECT_EQ("35", fmt::to_string(std::cref(num)));
}

0 comments on commit 8fac8c2

Please sign in to comment.