Don't use initializer lists.

This commit is contained in:
Victor Zverovich 2014-04-22 19:42:25 -07:00
parent 656a8378d1
commit 1104e73242
2 changed files with 20 additions and 27 deletions

View File

@ -1434,7 +1434,7 @@ TEST(FormatterTest, Examples) {
std::string path = "somefile"; std::string path = "somefile";
ReportError("File not found: {0}") << path; ReportError("File not found: {0}") << path;
#if FMT_USE_INITIALIZER_LIST #if FMT_USE_VARIADIC_TEMPLATES
EXPECT_THROW_MSG( EXPECT_THROW_MSG(
Format("The answer is {:d}", "forty-two"), FormatError, Format("The answer is {:d}", "forty-two"), FormatError,
"unknown format code 'd' for string"); "unknown format code 'd' for string");
@ -1501,12 +1501,12 @@ TEST(StrTest, Convert) {
EXPECT_EQ("2012-12-9", s); EXPECT_EQ("2012-12-9", s);
} }
#if FMT_USE_INITIALIZER_LIST && FMT_USE_VARIADIC_TEMPLATES #if FMT_USE_VARIADIC_TEMPLATES
TEST(FormatTest, Variadic) { TEST(FormatTest, Variadic) {
EXPECT_EQ("Hello, world!1", 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)); 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) { int main(int argc, char **argv) {
#ifdef _WIN32 #ifdef _WIN32

View File

@ -61,22 +61,12 @@
# define __has_builtin(x) 0 # define __has_builtin(x) 0
#endif #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 #ifndef FMT_USE_VARIADIC_TEMPLATES
# define FMT_USE_VARIADIC_TEMPLATES \ # define FMT_USE_VARIADIC_TEMPLATES \
(__has_feature(cxx_variadic_templates) || \ (__has_feature(cxx_variadic_templates) || \
(FMT_GCC_VERSION >= 404 && __cplusplus >= 201103) || _MSC_VER >= 1800) (FMT_GCC_VERSION >= 404 && __cplusplus >= 201103) || _MSC_VER >= 1800)
#endif #endif
#if FMT_USE_INITIALIZER_LIST
# include <initializer_list>
#endif
// Define FMT_USE_NOEXCEPT to make format use noexcept (C++11 feature). // Define FMT_USE_NOEXCEPT to make format use noexcept (C++11 feature).
#if FMT_USE_NOEXCEPT || __has_feature(cxx_noexcept) || \ #if FMT_USE_NOEXCEPT || __has_feature(cxx_noexcept) || \
(FMT_GCC_VERSION >= 408 && __cplusplus >= 201103) (FMT_GCC_VERSION >= 408 && __cplusplus >= 201103)
@ -1194,15 +1184,18 @@ class BasicFormatter {
BasicFormatter(BasicWriter<Char> &w, const Char *format = 0) BasicFormatter(BasicWriter<Char> &w, const Char *format = 0)
: writer_(&w), format_(format) {} : writer_(&w), format_(format) {}
#if FMT_USE_INITIALIZER_LIST #if FMT_USE_VARIADIC_TEMPLATES
// Constructs a formatter with formatting arguments. // Constructs a formatter with variable number of arguments.
BasicFormatter(BasicWriter<Char> &w, template<typename... Args>
const Char *format, std::initializer_list<Arg> args) BasicFormatter(
BasicWriter<Char> &w, const Char *format, const Args & ... args)
: writer_(&w), format_(format) { : writer_(&w), format_(format) {
// TODO: don't copy arguments std::size_t num_args = sizeof...(Args);
args_.reserve(args.size()); Arg arg_array[] = {args...};
for (const Arg &arg: args) // TODO: use array directly instead of copying
args_.push_back(arg); args_.reserve(num_args);
for (std::size_t i = 0; i < num_args; ++i)
args_.push_back(arg_array[i]);
} }
#endif #endif
@ -1529,21 +1522,21 @@ inline Formatter<ColorWriter> PrintColored(Color c, StringRef format) {
return f; return f;
} }
#if FMT_USE_INITIALIZER_LIST && FMT_USE_VARIADIC_TEMPLATES #if FMT_USE_VARIADIC_TEMPLATES
template<typename... Args> template<typename... Args>
std::string Format(const StringRef &format, const Args & ... args) { inline std::string Format(const StringRef &format, const Args & ... args) {
Writer w; Writer w;
BasicFormatter<char> f(w, format.c_str(), { args... }); BasicFormatter<char> f(w, format.c_str(), args...);
return fmt::str(f); return fmt::str(f);
} }
template<typename... Args> template<typename... Args>
std::wstring Format(const WStringRef &format, const Args & ... args) { inline std::wstring Format(const WStringRef &format, const Args & ... args) {
WWriter w; WWriter w;
BasicFormatter<wchar_t> f(w, format.c_str(), { args... }); BasicFormatter<wchar_t> f(w, format.c_str(), args...);
return fmt::str(f); return fmt::str(f);
} }
#endif // FMT_USE_INITIALIZER_LIST && FMT_USE_VARIADIC_TEMPLATES #endif // FMT_USE_VARIADIC_TEMPLATES
} }