From 997e2747eedaf17c5688d70814529c50e85633a6 Mon Sep 17 00:00:00 2001 From: Mathias Stearn Date: Thu, 30 May 2019 13:53:58 -0400 Subject: [PATCH] Better compiler errors for invalid ""_format usage In particular, this uses FMT_ENABLE_IF() to constrain the function to only valid combinations of the format string and arguments. This ensures that there is an actual error at the usage site, which puts the red squiggles on the incorrect code, rather than inside the library. --- include/fmt/format.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 8525639e..7bf9ca62 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3566,14 +3566,17 @@ namespace internal { # if FMT_USE_UDL_TEMPLATE template class udl_formatter { - public: template + FMT_CONSTEXPR11 static bool is_valid() { + return do_check_format_string( + basic_string_view( + std::initializer_list{CHARS...}.begin(), + sizeof...(CHARS))); + } + public: + template ())> std::basic_string operator()(const Args&... args) const { FMT_CONSTEXPR_DECL Char s[] = {CHARS..., '\0'}; - FMT_CONSTEXPR_DECL bool invalid_format = - do_check_format_string( - basic_string_view(s, sizeof...(CHARS))); - (void)invalid_format; return format(s, args...); } };