Skip to content

Commit

Permalink
format std::reference_wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
yfeldblum authored and vitaut committed Sep 17, 2024
1 parent 4197727 commit 07e7015
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
11 changes: 11 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 @@ -692,4 +693,14 @@ template <typename T, typename Char> struct formatter<std::complex<T>, Char> {
};

FMT_END_NAMESPACE

namespace std {

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

} // namespace std

#endif // FMT_STD_H_
5 changes: 5 additions & 0 deletions test/std-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,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 07e7015

Please sign in to comment.