Skip to content

Commit

Permalink
Detect types convertible to unformattable pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Nov 13, 2021
1 parent 094b66e commit 5380ff4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1370,8 +1370,11 @@ template <typename Context> struct arg_mapper {

// We use SFINAE instead of a const T* parameter to avoid conflicting with
// the C array overload.
template <typename T, FMT_ENABLE_IF(std::is_pointer<T>::value)>
FMT_CONSTEXPR auto map(T) -> unformattable_pointer {
template <
typename T,
FMT_ENABLE_IF(std::is_convertible<const T&, const void*>::value &&
!std::is_convertible<const T&, const char_type*>::value)>
FMT_CONSTEXPR auto map(const T&) -> unformattable_pointer {
return {};
}

Expand Down
6 changes: 6 additions & 0 deletions test/core-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,10 @@ template <> struct formatter<nonconst_formattable> {
};
FMT_END_NAMESPACE

struct convertible_to_pointer {
operator const int*() const { return nullptr; }
};

TEST(core_test, is_formattable) {
static_assert(fmt::is_formattable<signed char*>::value, "");
static_assert(fmt::is_formattable<unsigned char*>::value, "");
Expand Down Expand Up @@ -760,6 +764,8 @@ TEST(core_test, is_formattable) {
static_assert(!fmt::is_formattable<const nonconst_formattable&>::value, "");
#endif

static_assert(!fmt::is_formattable<convertible_to_pointer>::value, "");

static_assert(!fmt::is_formattable<signed char*, wchar_t>::value, "");
static_assert(!fmt::is_formattable<unsigned char*, wchar_t>::value, "");
static_assert(!fmt::is_formattable<const signed char*, wchar_t>::value, "");
Expand Down

0 comments on commit 5380ff4

Please sign in to comment.