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 <haje@eposaudio.com>
This commit is contained in:
Hans-Martin B. Jensen 2022-11-14 06:54:32 +01:00 committed by GitHub
parent d2e89c8b08
commit 7df30f91ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View File

@ -3888,7 +3888,8 @@ template <typename T> auto ptr(T p) -> const void* {
static_assert(std::is_pointer<T>::value, "");
return detail::bit_cast<const void*>(p);
}
template <typename T> auto ptr(const std::unique_ptr<T>& p) -> const void* {
template <typename T, typename Deleter>
auto ptr(const std::unique_ptr<T, Deleter>& p) -> const void* {
return p.get();
}
template <typename T> auto ptr(const std::shared_ptr<T>& p) -> const void* {

View File

@ -1508,6 +1508,12 @@ TEST(format_test, format_pointer) {
std::unique_ptr<int> 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<int, custom_deleter> upcd(new int(1));
EXPECT_EQ(fmt::format("{}", fmt::ptr(upcd.get())),
fmt::format("{}", fmt::ptr(upcd)));
std::shared_ptr<int> sp(new int(1));
EXPECT_EQ(fmt::format("{}", fmt::ptr(sp.get())),
fmt::format("{}", fmt::ptr(sp)));