这段代码无法编译。(error: no matching function for call to 'vformat(std::wstring&, fmt::v10::format_arg_store<fmt::v10::basic_format_context<std::back_insert_iterator<fmt::v10::detail::buffer<wchar_t> >, wchar_t>, int>)')
#include <fmt/xchar.h>
#include <string>
#include <iostream>
int main() {
int arg_1 = 12;
std::wstring format = L"Hello {}";
std::wstring test = fmt::vformat(format, fmt::make_wformat_args(arg_1));
std::wcout << test;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,这段代码可以成功编译。
#include <fmt/xchar.h>
#include <string>
#include <iostream>
int main() {
int arg_1 = 12;
std::string format = "Hello {}";
std::string test = fmt::vformat(format, fmt::make_format_args(arg_1));
std::cout << test;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 {fmt} 10.0.0 并包括 xchar.h,它的fmt::format工作原理与我对宽字符串的预期一样,看来我只使用fmt::vformat.
我尝试过在不同的配置中包含各种单独的 fmt 头文件(fmt/core.h、fmt/format.h、fmt/xchar.h)。这似乎毫无意义,因为 xchar.h 包含其他库头文件。
虽然我专门使用 MSVC,但我在 GCC 上尝试过并遇到了同样的问题。
我尝试过用它fmt::make_format_args来代替。
我还尝试指定 的fmt::wformat_context模板参数fmt::make_wformat_args。
我的具体用例是使用磁盘中的本地化文件,因此我无法在编译时知道格式字符串。
fmt::vformat您自己编写格式化函数时不应该使用except 。传递仅在运行时已知的格式字符串的正确方法是将其包装fmt::runtime并调用fmt::format:
#include <fmt/xchar.h>
#include <iostream>
int main() {
int arg_1 = 12;
std::wstring format = L"Hello {}";
std::wstring test = fmt::format(fmt::runtime(format), arg_1);
std::wcout << test;
}
Run Code Online (Sandbox Code Playgroud)
https://godbolt.org/z/4WKd575o7