From 5380ff4d8866d2f37d706859c6223bc22d17dc98 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 13 Nov 2021 08:26:27 -0800 Subject: [PATCH] Detect types convertible to unformattable pointers --- include/fmt/core.h | 7 +++++-- test/core-test.cc | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 01cf34cf1d4f..bb56da7fffd6 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -1370,8 +1370,11 @@ template struct arg_mapper { // We use SFINAE instead of a const T* parameter to avoid conflicting with // the C array overload. - template ::value)> - FMT_CONSTEXPR auto map(T) -> unformattable_pointer { + template < + typename T, + FMT_ENABLE_IF(std::is_convertible::value && + !std::is_convertible::value)> + FMT_CONSTEXPR auto map(const T&) -> unformattable_pointer { return {}; } diff --git a/test/core-test.cc b/test/core-test.cc index 5cf9dbd8c869..da3bc0f96cd1 100644 --- a/test/core-test.cc +++ b/test/core-test.cc @@ -733,6 +733,10 @@ template <> struct formatter { }; FMT_END_NAMESPACE +struct convertible_to_pointer { + operator const int*() const { return nullptr; } +}; + TEST(core_test, is_formattable) { static_assert(fmt::is_formattable::value, ""); static_assert(fmt::is_formattable::value, ""); @@ -760,6 +764,8 @@ TEST(core_test, is_formattable) { static_assert(!fmt::is_formattable::value, ""); #endif + static_assert(!fmt::is_formattable::value, ""); + static_assert(!fmt::is_formattable::value, ""); static_assert(!fmt::is_formattable::value, ""); static_assert(!fmt::is_formattable::value, "");