将 fmtlib 与 wofstream 一起使用的规范方法是什么

What's the canonical way to use the fmtlib with wofstream

我们正在切换到 fmt 库,旨在重写许多基于宽字符输出流的格式。

我发现 fmt::output_file 其中 returns 是一个 class ostream : private detail::buffer<char> 实例,但似乎不支持 wchar_t。

在宽字符输出流场景中使用格式库的规范方式是什么?

我能想到的唯一方法就是手动打开一个std::wofstream然后使用fmt::format。 这是在路线图上还是我遗漏了什么?

fmt/ostream.h 提供了一个 fmt::print 重载,可以写入 wofstream:

#include <fmt/ostream.h>
#include <fstream>

int main() {
  std::wofstream ofs("foo");
  fmt::print(ofs, L"The answer is {}.\n", 42);
}

godbolt

也就是说,如果您关心便携性和性能,我建议避免使用 wchar_t