如何获得 fmt::format 与wchar_t一起使用?
How to get fmt::format to work with wchar_t?
我想让 fmt::format 从 EA 的 STL 返回一个字符串,但是当我尝试从文档中修改这段代码时——这工作正常:
// 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...));
}
要使用 wchar_t 而不是 char:
// 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...));
}
我得到:
2>fmtcore.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>fmtcore.h(1219,1): error C2665: : basic_format_args<wformat_context>(std::forward<Args>(args)...) {}
2>fmtcore.h(1219,1): error C2665: ^ (compiling source file ..coretypedefs.cpp)
2>fmtcore.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>fmtcore.h(1207,1): message : };
2>fmtcore.h(1207,1): message : ^ (compiling source file ..coretypedefs.cpp)
2>fmtcore.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>fmtcore.h(1207,1): message : };
2>fmtcore.h(1207,1): message : ^ (compiling source file ..coretypedefs.cpp)
2>fmtcore.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>fmtcore.h(1189,3): message : with
2>fmtcore.h(1189,3): message : [
2>fmtcore.h(1189,3): message : Context=fmt::v5::wformat_context
2>fmtcore.h(1189,3): message : ]
2>fmtcore.h(1189,3): message : basic_format_args(const format_arg* args, int count)
2>fmtcore.h(1189,3): message : ^ (compiling source file ..coretypedefs.cpp)
2>fmtcore.h(1171,3): message : or 'fmt::v5::basic_format_args<fmt::v5::wformat_context>::basic_format_args(void)'
2>fmtcore.h(1171,3): message : basic_format_args() : types_(0) {}
2>fmtcore.h(1171,3): message : ^ (compiling source file ..coretypedefs.cpp)
2>fmtcore.h(1219,1): message : while trying to match the argument list '(fmt::v5::format_args)'
2>fmtcore.h(1219,1): message : : basic_format_args<wformat_context>(std::forward<Args>(args)...) {}
2>fmtcore.h(1219,1): message : ^ (compiling source file ..coretypedefs.cpp)
或者,如果我尝试转换为 char 然后转换出来,我无法让它适用于多个参数:
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 : ^
我还发现提到了 fmt::MemoryWriter,它可能有助于我格式化为 wchar_t[],但看起来它不再可用了?
不知道还能尝试什么。
更新
在用新鲜的眼光看了之后,我意识到我并没有包装fmt_msg2——所以现在我有了这个:
在 typedefs.h 文件中:
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...));
}
在客户端.cpp文件中:
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);
但是在Visual Studio 16.2.0上,我收到以下错误:
core.h(687,39): error C2039: 'char_type': is not a member of 'fmt::v5::wformat_args'
core.h(687,39): error C2039: using char_type = typename Context::char_type;
core.h(687,39): error C2039: ^ (compiling source file client.cpp)
core.h(1216): message : see declaration of 'fmt::v5::wformat_args'
core.h(1216): message : struct wformat_args : basic_format_args<wformat_context> { (compiling source file client.cpp)
core.h(1095): message : see reference to class template instantiation 'fmt::v5::internal::value<Context>' being compiled
core.h(1095): message : with
core.h(1095): message : [
core.h(1095): message : Context=fmt::v5::wformat_args
core.h(1095): message : ]
core.h(1095): message : value_type data_[num_args + (!is_packed || num_args == 0 ? 1 : 0)]; (compiling source file client.cpp)
typedefs.h(119): message : see reference to class template instantiation 'fmt::v5::format_arg_store<fmt::v5::wformat_args,int,int>' being compiled
typedefs.h(119): message : fmt_msg3(const ea::wstring &format, const Args &... args) { (compiling source file client.cpp)
client.cpp(757): message : see reference to function template instantiation 'eastl::wstring fmt_msg3<int,int>(const eastl::wstring &,const int &,const int &)' being compiled
client.cpp(757): message : wstring s = fmt_msg3(L"hey {} {}", 24, 58);
似乎您需要使用wformat_args
并为make_format_args
明确指定wformat_context
:
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...));
}
相关文章:
- 如何将enable-if与模板参数和参数包一起使用
- 如何将PERF_AMPLE_READ与mmap一起使用
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将C++中的库和头与MinGW一起使用
- MacOS通过在莫哈韦"wchar.h"下破碎的自制啤酒发出叮当声
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 为什么我不能将 rand() 与数组的大小一起使用?
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 不能将复制初始化与隐式转换的多个步骤一起使用
- 将fold表达式与std::一起用于两个元组
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 将 std::allocate_shared 与多态资源分配器一起使用
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 将 exprtk 与自定义类的对象一起使用
- 将 std::set 与基于键的比较器一起使用
- 将 C++ 类与 Rcpp 一起使用,从 C 或 R 修改它
- 如何将 Eigen::Ref 与 pybind11 一起使用?
- 如何将AERT_Allocate与 std:vector 一起使用
- 如何使我的 sizeof sum 结构与空参数包一起工作