From cea3c20747fc824dd8234df314549bf4c053168d Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 13 Jan 2018 07:02:45 -0800 Subject: [PATCH] Give a better error message for function pointers (#633) --- include/fmt/core.h | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index a1fddb73..c203ea76 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -268,9 +268,6 @@ struct formatter; namespace internal { -template -inline const T *as_const(T *p) { return p; } - // A helper function to suppress bogus "conditional expression is constant" // warnings. template @@ -508,19 +505,11 @@ class value { value(basic_string_view s) { set_string(s); } value(const std::basic_string &s) { set_string(s); } - // Formatting of arbitrary pointers is disallowed. If you want to output a - // pointer cast it to "void *" or "const void *". In particular, this forbids - // formatting of "[const] volatile char *" which is printed as bool by - // iostreams. template - value(const T *p) { - static_assert(std::is_same::value, - "formatting of non-void pointers is disallowed"); - set(pointer, p); - } + value(T *p) { set_pointer(p); } template - value(T *p) : value(as_const(p)) {} + value(const T *p) { set_pointer(p); } value(std::nullptr_t) { pointer = nullptr; } @@ -573,6 +562,18 @@ class value { set(field, str); } + // Formatting of arbitrary pointers is disallowed. If you want to output a + // pointer cast it to "void *" or "const void *". In particular, this forbids + // formatting of "[const] volatile char *" which is printed as bool by + // iostreams. + template + void set_pointer(T *p) { + using type = typename std::remove_const::type; + static_assert(std::is_same::value, + "formatting of non-void pointers is disallowed"); + set(pointer, p); + } + // Formats an argument of a custom type, such as a user-defined class. template static void format_custom_arg(const void *arg, Context &ctx) {