Give a better error message for function pointers (#633)

This commit is contained in:
Victor Zverovich 2018-01-13 07:02:45 -08:00
parent 232ceabbc3
commit cea3c20747

View File

@ -268,9 +268,6 @@ struct formatter;
namespace internal { namespace internal {
template <typename T>
inline const T *as_const(T *p) { return p; }
// A helper function to suppress bogus "conditional expression is constant" // A helper function to suppress bogus "conditional expression is constant"
// warnings. // warnings.
template <typename T> template <typename T>
@ -508,19 +505,11 @@ class value {
value(basic_string_view<char_type> s) { set_string(s); } value(basic_string_view<char_type> s) { set_string(s); }
value(const std::basic_string<char_type> &s) { set_string(s); } value(const std::basic_string<char_type> &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 <typename T> template <typename T>
value(const T *p) { value(T *p) { set_pointer(p); }
static_assert(std::is_same<T, void>::value,
"formatting of non-void pointers is disallowed");
set<POINTER>(pointer, p);
}
template <typename T> template <typename T>
value(T *p) : value(as_const(p)) {} value(const T *p) { set_pointer(p); }
value(std::nullptr_t) { pointer = nullptr; } value(std::nullptr_t) { pointer = nullptr; }
@ -573,6 +562,18 @@ class value {
set<CSTRING>(field, str); set<CSTRING>(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 <typename T>
void set_pointer(T *p) {
using type = typename std::remove_const<T>::type;
static_assert(std::is_same<type, void>::value,
"formatting of non-void pointers is disallowed");
set<POINTER>(pointer, p);
}
// Formats an argument of a custom type, such as a user-defined class. // Formats an argument of a custom type, such as a user-defined class.
template <typename T> template <typename T>
static void format_custom_arg(const void *arg, Context &ctx) { static void format_custom_arg(const void *arg, Context &ctx) {