如何获取fmt :: format与wchar_t配合使用?

时间:2019-08-11 03:49:00

标签: c++ fmt

我想让fmt :: format从EA的STL返回一个wstring,但是当我尝试从文档中修改此代码时,效果很好:

InventoryViewHolder

使用wchar_t代替char:

// Prints formatted error message.
void
vreport_error(const char *format, fmt::format_args args) {
    fmt::print("Error: ");
    fmt::vprint(format, args);
}

template <typename... Args>
void
report_error(const char *format, const Args &... args) {
    vreport_error(format, fmt::make_format_args(args...));
}

我得到:

// Prints formatted error message.
void vreport_error2(const wchar_t *format, fmt::format_args args);
template <typename... Args>
void
report_error2(const wchar_t *format, const Args &... args) {
    vreport_error2(format, fmt::make_format_args(args...));
}

或者,如果我尝试转换为char然后转换为char,则无法使它用于多个参数:

2>fmt\core.h(1219,1): error C2665:  'fmt::v5::basic_format_args<fmt::v5::wformat_context>::basic_format_args': none of the 4 overloads could convert all the argument types
2>fmt\core.h(1219,1): error C2665:       : basic_format_args<wformat_context>(std::forward<Args>(args)...) {}
2>fmt\core.h(1219,1): error C2665: ^ (compiling source file ..\core\typedefs.cpp)
2>fmt\core.h(1207,1): message :  could be 'fmt::v5::basic_format_args<fmt::v5::wformat_context>::basic_format_args(fmt::v5::basic_format_args<fmt::v5::wformat_context> &&)'
2>fmt\core.h(1207,1): message : };
2>fmt\core.h(1207,1): message : ^ (compiling source file ..\core\typedefs.cpp)
2>fmt\core.h(1207,1): message :  or       'fmt::v5::basic_format_args<fmt::v5::wformat_context>::basic_format_args(const fmt::v5::basic_format_args<fmt::v5::wformat_context> &)'
2>fmt\core.h(1207,1): message : };
2>fmt\core.h(1207,1): message : ^ (compiling source file ..\core\typedefs.cpp)
2>fmt\core.h(1189,3): message :  or       'fmt::v5::basic_format_args<fmt::v5::wformat_context>::basic_format_args(const fmt::v5::basic_format_arg<Context> *,int)'
2>fmt\core.h(1189,3): message :         with
2>fmt\core.h(1189,3): message :         [
2>fmt\core.h(1189,3): message :             Context=fmt::v5::wformat_context
2>fmt\core.h(1189,3): message :         ]
2>fmt\core.h(1189,3): message :   basic_format_args(const format_arg* args, int count)
2>fmt\core.h(1189,3): message :   ^ (compiling source file ..\core\typedefs.cpp)
2>fmt\core.h(1171,3): message :  or       'fmt::v5::basic_format_args<fmt::v5::wformat_context>::basic_format_args(void)'
2>fmt\core.h(1171,3): message :   basic_format_args() : types_(0) {}
2>fmt\core.h(1171,3): message :   ^ (compiling source file ..\core\typedefs.cpp)
2>fmt\core.h(1219,1): message :  while trying to match the argument list '(fmt::v5::format_args)'
2>fmt\core.h(1219,1): message :       : basic_format_args<wformat_context>(std::forward<Args>(args)...) {}
2>fmt\core.h(1219,1): message : ^ (compiling source file ..\core\typedefs.cpp)

我还发现提到了fmt :: MemoryWriter,它可以帮助我将其格式化为wchar_t [],但似乎不再可用了?

不确定要尝试什么。

更新

用新鲜的眼睛看了一下之后,我意识到我没有包装fmt_msg2,所以现在我有了:

在typedefs.h文件中:

wstring
fmt_msg2(const wchar_t *msg, fmt::format_args args) {
  std::string s  = utf8_from_utf16(msg).c_str();
  std::string s_ = fmt::vformat(s, args);
  return utf16_from_utf8(s_.data());
}

wstring s = fmt_msg2(L"hey {}", 24); // works
wstring s = fmt_msg2(L"hey {} {}", 24, 58); // error

1>client.cpp
1>client.cpp(757,44): error C2660:  'fmt_msg2': function does not take 3 arguments
1>client.cpp(757,44): error C2660:   wstring s = fmt_msg2(L"hey {} {}", 24, 58);
1>client.cpp(757,44): error C2660:                                            ^
1>typedefs.h(109,9): message :  see declaration of 'fmt_msg2'
1>typedefs.h(109,9): message : wstring fmt_msg2(wchar_t cc *msg, fmt::format_args args);
1>typedefs.h(109,9): message :         ^

在client.cpp文件中:

ea::wstring vfmt_msg3(const ea::wstring &msg, fmt::wformat_args args);

template <typename... Args>
ea::wstring
fmt_msg3(const ea::wstring &format, const Args &... args) {
    return vfmt_msg3(format, fmt::make_format_args<fmt::wformat_args>(args...));
}

但是在Visual Studio 16.2.0上,出现以下错误:

ea::wstring
vfmt_msg3(const ea::wstring &msg, fmt::wformat_args args) {
    std::wstring s_ = fmt::vformat(std::wstring(msg.data()), args);
    return ea::wstring(s_.data());
}

wstring s = fmt_msg3(L"hey {} {}", 24, 58);

1 个答案:

答案 0 :(得分:4)

似乎您需要使用wformat_args并为wformat_context明确指定make_format_args

void vreport_error2(const wchar_t *format, fmt::wformat_args args)  {
  fmt::print("Error: ");
  fmt::vprint(format, args);
}
template <typename... Args>
void
report_error2(const wchar_t *format, const Args &... args) {
  vreport_error2(format, fmt::make_format_args<fmt::wformat_context>(args...));
}