diff --git a/format-test.cc b/format-test.cc index 2660470b..9c2bc11c 100644 --- a/format-test.cc +++ b/format-test.cc @@ -1434,7 +1434,7 @@ TEST(FormatterTest, Examples) { std::string path = "somefile"; ReportError("File not found: {0}") << path; -#if FMT_USE_INITIALIZER_LIST +#if FMT_USE_VARIADIC_TEMPLATES EXPECT_THROW_MSG( Format("The answer is {:d}", "forty-two"), FormatError, "unknown format code 'd' for string"); @@ -1501,12 +1501,12 @@ TEST(StrTest, Convert) { EXPECT_EQ("2012-12-9", s); } -#if FMT_USE_INITIALIZER_LIST && FMT_USE_VARIADIC_TEMPLATES +#if FMT_USE_VARIADIC_TEMPLATES TEST(FormatTest, Variadic) { EXPECT_EQ("Hello, world!1", Format("Hello, {}!{}", "world", 1)); EXPECT_EQ(L"Hello, world!1", Format(L"Hello, {}!{}", L"world", 1)); } -#endif // FMT_USE_INITIALIZER_LIST && FMT_USE_VARIADIC_TEMPLATES +#endif // FMT_USE_VARIADIC_TEMPLATES int main(int argc, char **argv) { #ifdef _WIN32 diff --git a/format.h b/format.h index e837ba4b..c22cb850 100644 --- a/format.h +++ b/format.h @@ -61,22 +61,12 @@ # define __has_builtin(x) 0 #endif -#ifndef FMT_USE_INITIALIZER_LIST -# define FMT_USE_INITIALIZER_LIST \ - (__has_feature(cxx_generalized_initializers) || \ - (FMT_GCC_VERSION >= 404 && __cplusplus >= 201103) || _MSC_VER >= 1800) -#endif - #ifndef FMT_USE_VARIADIC_TEMPLATES # define FMT_USE_VARIADIC_TEMPLATES \ (__has_feature(cxx_variadic_templates) || \ (FMT_GCC_VERSION >= 404 && __cplusplus >= 201103) || _MSC_VER >= 1800) #endif -#if FMT_USE_INITIALIZER_LIST -# include -#endif - // Define FMT_USE_NOEXCEPT to make format use noexcept (C++11 feature). #if FMT_USE_NOEXCEPT || __has_feature(cxx_noexcept) || \ (FMT_GCC_VERSION >= 408 && __cplusplus >= 201103) @@ -1194,15 +1184,18 @@ class BasicFormatter { BasicFormatter(BasicWriter &w, const Char *format = 0) : writer_(&w), format_(format) {} -#if FMT_USE_INITIALIZER_LIST - // Constructs a formatter with formatting arguments. - BasicFormatter(BasicWriter &w, - const Char *format, std::initializer_list args) +#if FMT_USE_VARIADIC_TEMPLATES + // Constructs a formatter with variable number of arguments. + template + BasicFormatter( + BasicWriter &w, const Char *format, const Args & ... args) : writer_(&w), format_(format) { - // TODO: don't copy arguments - args_.reserve(args.size()); - for (const Arg &arg: args) - args_.push_back(arg); + std::size_t num_args = sizeof...(Args); + Arg arg_array[] = {args...}; + // TODO: use array directly instead of copying + args_.reserve(num_args); + for (std::size_t i = 0; i < num_args; ++i) + args_.push_back(arg_array[i]); } #endif @@ -1529,21 +1522,21 @@ inline Formatter PrintColored(Color c, StringRef format) { return f; } -#if FMT_USE_INITIALIZER_LIST && FMT_USE_VARIADIC_TEMPLATES +#if FMT_USE_VARIADIC_TEMPLATES template -std::string Format(const StringRef &format, const Args & ... args) { +inline std::string Format(const StringRef &format, const Args & ... args) { Writer w; - BasicFormatter f(w, format.c_str(), { args... }); + BasicFormatter f(w, format.c_str(), args...); return fmt::str(f); } template -std::wstring Format(const WStringRef &format, const Args & ... args) { +inline std::wstring Format(const WStringRef &format, const Args & ... args) { WWriter w; - BasicFormatter f(w, format.c_str(), { args... }); + BasicFormatter f(w, format.c_str(), args...); return fmt::str(f); } -#endif // FMT_USE_INITIALIZER_LIST && FMT_USE_VARIADIC_TEMPLATES +#endif // FMT_USE_VARIADIC_TEMPLATES }