Skip to content

Commit

Permalink
fmt::ptr: support unique_ptr and shared_ptr.
Browse files Browse the repository at this point in the history
  • Loading branch information
sighingnow committed Apr 18, 2019
1 parent d306585 commit e522929
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3290,6 +3290,12 @@ typename Context::iterator vformat_to(
// Example:
// auto s = format("{}", ptr(p));
template <typename T> inline const void* ptr(const T* p) { return p; }
template <typename T> inline const void* ptr(const std::unique_ptr<T>& p) {
return p.get();
}
template <typename T> inline const void* ptr(const std::shared_ptr<T>& p) {
return p.get();
}

template <typename It, typename Char> struct arg_join {
It begin;
Expand Down
4 changes: 4 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1600,6 +1600,10 @@ TEST(FormatterTest, FormatPointer) {
EXPECT_EQ("0x" + std::string(sizeof(void*) * CHAR_BIT / 4, 'f'),
format("{0}", reinterpret_cast<void*>(~uintptr_t())));
EXPECT_EQ("0x1234", format("{}", fmt::ptr(reinterpret_cast<int*>(0x1234))));
std::unique_ptr<int> up(new int(1));
EXPECT_EQ(format("{}", fmt::ptr(up.get())), format("{}", fmt::ptr(up)));
std::shared_ptr<int> sp(new int(1));
EXPECT_EQ(format("{}", fmt::ptr(sp.get())), format("{}", fmt::ptr(sp)));
#if FMT_USE_NULLPTR
EXPECT_EQ("0x0", format("{}", FMT_NULL));
#endif
Expand Down

0 comments on commit e522929

Please sign in to comment.