From 6b20863918cccec4773273d577c2bcf04132ea30 Mon Sep 17 00:00:00 2001 From: "HE, Tao" Date: Thu, 18 Apr 2019 13:48:44 +0800 Subject: [PATCH] fmt::ptr: support unique_ptr and shared_ptr. --- include/fmt/format.h | 6 ++++++ test/format-test.cc | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/include/fmt/format.h b/include/fmt/format.h index 0fa38d0b4877..e9f29518b17b 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3290,6 +3290,12 @@ typename Context::iterator vformat_to( // Example: // auto s = format("{}", ptr(p)); template inline const void* ptr(const T* p) { return p; } +template inline const void* ptr(const std::unique_ptr& p) { + return p.get(); +} +template inline const void* ptr(const std::shared_ptr& p) { + return p.get(); +} template struct arg_join { It begin; diff --git a/test/format-test.cc b/test/format-test.cc index e201ddfb49c1..866f2ae6bfed 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1600,6 +1600,10 @@ TEST(FormatterTest, FormatPointer) { EXPECT_EQ("0x" + std::string(sizeof(void*) * CHAR_BIT / 4, 'f'), format("{0}", reinterpret_cast(~uintptr_t()))); EXPECT_EQ("0x1234", format("{}", fmt::ptr(reinterpret_cast(0x1234)))); + std::unique_ptr up(new int(1)); + EXPECT_EQ(format("{}", fmt::ptr(up.get())), format("{}", fmt::ptr(up))); + std::shared_ptr 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