Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting of function pointers, member function pointers, member object pointers... #2610

Merged
merged 6 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1372,8 +1372,11 @@ template <typename Context> struct arg_mapper {
// the C array overload.
template <
typename T,
FMT_ENABLE_IF(std::is_convertible<const T&, const void*>::value &&
!std::is_convertible<const T&, const char_type*>::value)>
FMT_ENABLE_IF(
std::is_member_pointer<T>::value ||
std::is_function<typename std::remove_pointer<T>::type>::value ||
(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/compile-error-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ expect_compile_error("
fmt::format(\"{}\", S());
")

# Formatting a function
expect_compile_error("
void (*f)();
fmt::format(\"{}\", f);
")

# Make sure that compiler features detected in the header
# match the features detected in CMake.
if (SUPPORTS_USER_DEFINED_LITERALS)
Expand Down
7 changes: 7 additions & 0 deletions test/core-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,13 @@ TEST(core_test, is_formattable) {
static_assert(!fmt::is_formattable<unsigned char*, wchar_t>::value, "");
static_assert(!fmt::is_formattable<const signed char*, wchar_t>::value, "");
static_assert(!fmt::is_formattable<const unsigned char*, wchar_t>::value, "");

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

struct s;

static_assert(!fmt::is_formattable<int(s::*)>::value, "");
static_assert(!fmt::is_formattable<int (s::*)()>::value, "");
}

TEST(core_test, format) { EXPECT_EQ(fmt::format("{}", 42), "42"); }
Expand Down