From 17d621f86ff9b0d2d86f21261e95f69652491f81 Mon Sep 17 00:00:00 2001 From: jdale88 Date: Thu, 6 Mar 2014 20:06:44 +0000 Subject: [PATCH] Added variadic versions for Format that act as a proxy for providing an initialiser list to a BasicFormatter. Updated the Variadic test to use these new functions and verified that the tests passed. See https://github.com/vitaut/format/issues/25 --- format-test.cc | 15 ++++----------- format.h | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/format-test.cc b/format-test.cc index e5ee65c9..f20d3fa9 100644 --- a/format-test.cc +++ b/format-test.cc @@ -1485,19 +1485,12 @@ TEST(StrTest, Convert) { EXPECT_EQ("2012-12-9", s); } -#if FMT_USE_INITIALIZER_LIST -template -inline std::string Format(const StringRef &format, const Args & ... args) { - Writer w; - fmt::BasicFormatter f(w, format.c_str(), {args...}); - return fmt::str(f); -} - +#if FMT_USE_INITIALIZER_LIST && FMT_USE_VARIADIC_TEMPLATES TEST(FormatTest, Variadic) { - Writer w; - EXPECT_EQ("Hello, world!1", str(Format("Hello, {}!{}", "world", 1))); + 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 +#endif // FMT_USE_INITIALIZER_LIST && FMT_USE_VARIADIC_TEMPLATES int main(int argc, char **argv) { #ifdef _WIN32 diff --git a/format.h b/format.h index 62dae6a5..fd35afea 100644 --- a/format.h +++ b/format.h @@ -61,6 +61,12 @@ (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 @@ -1491,6 +1497,23 @@ inline Formatter PrintColored(Color c, StringRef format) { Formatter f(format, ColorWriter(c)); return f; } + +#if FMT_USE_INITIALIZER_LIST && FMT_USE_VARIADIC_TEMPLATES +template +std::string Format(const StringRef &format, const Args & ... args) { + Writer w; + BasicFormatter f(w, format.c_str(), { args... }); + return fmt::str(f); +} + +template +std::wstring Format(const WStringRef &format, const Args & ... args) { + WWriter w; + BasicFormatter f(w, format.c_str(), { args... }); + return fmt::str(f); +} +#endif // FMT_USE_INITIALIZER_LIST && FMT_USE_VARIADIC_TEMPLATES + } #if _MSC_VER