fmt::ptr: support unique_ptr and shared_ptr.

This commit is contained in:
HE, Tao 2019-04-18 13:48:44 +08:00 committed by Victor Zverovich
parent d306585a3f
commit 6b20863918
2 changed files with 10 additions and 0 deletions

View File

@ -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;

View File

@ -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