From c3538a1eee93f3eb48e11272e0467b082b529888 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 30 Sep 2018 22:19:32 -0700 Subject: [PATCH] Simplify variadic functions further --- include/fmt/core.h | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 5572e682..a95c8e95 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -1068,8 +1068,6 @@ class format_arg_store { format_arg_store(const Args &... args) : data_{internal::make_arg(args)...} {} #endif - - basic_format_args operator*() const { return *this; } }; #if !FMT_USE_CONSTEXPR11 @@ -1303,6 +1301,18 @@ template typename std::enable_if::value>::type check_format_string(S); +template +struct checked_format_args: format_arg_store< + typename buffer_context::type, Args...> { + checked_format_args(const S &format_str, const Args &... args): + format_arg_store< + typename buffer_context::type, Args...>(args...) { + internal::check_format_string(format_str); + } + basic_format_args::type> + operator*() const { return *this; } +}; + template std::basic_string vformat( basic_string_view format_str, @@ -1350,10 +1360,9 @@ inline typename std::enable_if< std::back_insert_iterator>::type format_to(std::back_insert_iterator out, const S &format_str, const Args &... args) { - internal::check_format_string(format_str); - format_arg_store< - typename buffer_context::type, Args...> as(args...); - return vformat_to(out, basic_string_view(format_str), as); + return vformat_to( + out, basic_string_view(format_str), + *internal::checked_format_args(format_str, args...)); } template @@ -1377,13 +1386,9 @@ inline std::basic_string vformat( template inline std::basic_string format( const S &format_str, const Args &... args) { - internal::check_format_string(format_str); - // This should be just - // return vformat(format_str, make_format_args(args...)); - // but gcc has trouble optimizing the latter, so break it down. - format_arg_store< - typename buffer_context::type, Args...> as(args...); - return internal::vformat(basic_string_view(format_str), *as); + return internal::vformat( + basic_string_view(format_str), + *internal::checked_format_args(format_str, args...)); } FMT_API void vprint(std::FILE *f, string_view format_str, format_args args); @@ -1403,10 +1408,8 @@ FMT_API void vprint(std::FILE *f, wstring_view format_str, wformat_args args); template inline typename std::enable_if::value>::type print(std::FILE *f, const S &format_str, const Args &... args) { - internal::check_format_string(format_str); - format_arg_store< - typename buffer_context::type, Args...> as(args...); - vprint(f, basic_string_view(format_str), as); + vprint(f, basic_string_view(format_str), + *internal::checked_format_args(format_str, args...)); } FMT_API void vprint(string_view format_str, format_args args); @@ -1424,10 +1427,8 @@ FMT_API void vprint(wstring_view format_str, wformat_args args); template inline typename std::enable_if::value>::type print(const S &format_str, const Args &... args) { - internal::check_format_string(format_str); - format_arg_store< - typename buffer_context::type, Args...> as(args...); - vprint(basic_string_view(format_str), as); + vprint(basic_string_view(format_str), + *internal::checked_format_args(format_str, args...)); } FMT_END_NAMESPACE