From 0a4dd1e9679dc2d380747c28e481b1d9d12b6038 Mon Sep 17 00:00:00 2001 From: "Hans-Martin B. Jensen" Date: Sat, 12 Nov 2022 17:58:55 +0100 Subject: [PATCH 1/2] Format unique_ptr with custom deleter Added deleter type to fmt::ptr unique_ptr overload. Deleter type is part of the unique_ptr type. --- include/fmt/format.h | 2 +- test/format-test.cc | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 0c3231b5f87e..24387ccdd02d 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3888,7 +3888,7 @@ template auto ptr(T p) -> const void* { static_assert(std::is_pointer::value, ""); return detail::bit_cast(p); } -template auto ptr(const std::unique_ptr& p) -> const void* { +template auto ptr(const std::unique_ptr& p) -> const void* { return p.get(); } template auto ptr(const std::shared_ptr& p) -> const void* { diff --git a/test/format-test.cc b/test/format-test.cc index 4db3c9c49356..49b5f54107d6 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1508,6 +1508,12 @@ TEST(format_test, format_pointer) { std::unique_ptr up(new int(1)); EXPECT_EQ(fmt::format("{}", fmt::ptr(up.get())), fmt::format("{}", fmt::ptr(up))); + struct custom_deleter { + void operator()(int* p) const { delete p; } + }; + std::unique_ptr upcd(new int(1)); + EXPECT_EQ(fmt::format("{}", fmt::ptr(upcd.get())), + fmt::format("{}", fmt::ptr(upcd))); std::shared_ptr sp(new int(1)); EXPECT_EQ(fmt::format("{}", fmt::ptr(sp.get())), fmt::format("{}", fmt::ptr(sp))); From ca19cc67c2989abdc1696426c4e20fb5a69ddd6a Mon Sep 17 00:00:00 2001 From: "Hans-Martin B. Jensen" Date: Sun, 13 Nov 2022 20:32:32 +0100 Subject: [PATCH 2/2] Review: apply clang-format --- include/fmt/format.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 24387ccdd02d..98bfade459d3 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3888,7 +3888,8 @@ template auto ptr(T p) -> const void* { static_assert(std::is_pointer::value, ""); return detail::bit_cast(p); } -template auto ptr(const std::unique_ptr& p) -> const void* { +template +auto ptr(const std::unique_ptr& p) -> const void* { return p.get(); } template auto ptr(const std::shared_ptr& p) -> const void* {