From ed490145d8ea1cf5f0d272abe2f9a230bfa54d56 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 25 Feb 2015 16:03:00 -0800 Subject: [PATCH] Improve error reporting when trying to format wide char into narrow string --- format.h | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/format.h b/format.h index ad551356..e15baeb8 100644 --- a/format.h +++ b/format.h @@ -745,16 +745,16 @@ struct Arg : Value { template struct None {}; -// A helper class template to enable or disable overloads taking wide strings -// in MakeValue. +// A helper class template to enable or disable overloads taking wide +// characters and strings in MakeValue. template -struct WStringHelper { +struct WCharHelper { typedef None Supported; typedef T Unsupported; }; template -struct WStringHelper { +struct WCharHelper { typedef T Supported; typedef None Unsupported; }; @@ -774,12 +774,14 @@ class MakeValue : public Value { MakeValue(T *value); // The following methods are private to disallow formatting of wide - // strings into narrow strings as in fmt::format("{}", L"test"). + // characters and strings into narrow strings as in + // fmt::format("{}", L"test"); // To fix this, use a wide format string: fmt::format(L"{}", L"test"). - MakeValue(typename WStringHelper::Unsupported); - MakeValue(typename WStringHelper::Unsupported); - MakeValue(typename WStringHelper::Unsupported); - MakeValue(typename WStringHelper::Unsupported); + MakeValue(typename WCharHelper::Unsupported); + MakeValue(typename WCharHelper::Unsupported); + MakeValue(typename WCharHelper::Unsupported); + MakeValue(typename WCharHelper::Unsupported); + MakeValue(typename WCharHelper::Unsupported); void set_string(StringRef str) { string.value = str.c_str(); @@ -845,8 +847,8 @@ class MakeValue : public Value { FMT_MAKE_VALUE(unsigned char, int_value, CHAR) FMT_MAKE_VALUE(char, int_value, CHAR) - MakeValue(wchar_t value) { - int_value = internal::CharTraits::convert(value); + MakeValue(typename WCharHelper::Supported value) { + int_value = value; } static uint64_t type(wchar_t) { return Arg::CHAR; } @@ -862,7 +864,7 @@ class MakeValue : public Value { FMT_MAKE_STR_VALUE(StringRef, STRING) #define FMT_MAKE_WSTR_VALUE(Type, TYPE) \ - MakeValue(typename WStringHelper::Supported value) { \ + MakeValue(typename WCharHelper::Supported value) { \ set_string(value); \ } \ static uint64_t type(Type) { return Arg::TYPE; }