From 7df30f91aee5444a733cec0b911d21cebdeb62ae Mon Sep 17 00:00:00 2001 From: "Hans-Martin B. Jensen" <32485905+hmbj@users.noreply.github.com> Date: Mon, 14 Nov 2022 06:54:32 +0100 Subject: [PATCH] Format unique_ptr with custom deleter (#3177) * Format unique_ptr with custom deleter Added deleter type to fmt::ptr unique_ptr overload. Deleter type is part of the unique_ptr type. * Review: apply clang-format Co-authored-by: Hans-Martin B. Jensen --- include/fmt/format.h | 3 ++- test/format-test.cc | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 0c3231b5f87e..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* { 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)));