c++fmtwofstream

What's the canonical way to use {fmt} with wofstream?


We're switching to the {fmt} library and aim to rewrite many wide character output stream based formatting.

I found fmt::output_file which returns a class ostream : private detail::buffer<char> instance but it looks like there's no support for wchar_t.

What's the canonical way to use the format library in wide character output stream scenarios?

The only way I can think of is to manually open a std::wofstream and use ``mt::format`. Is this on a roadmap or am I missing something?


Solution

  • fmt/ostream.h provides an fmt::print overload that can write to wofstream:

    #include <fmt/ostream.h>
    #include <fstream>
    
    int main() {
      std::wofstream ofs("foo");
      fmt::print(ofs, L"The answer is {}.\n", 42);
    }
    

    godbolt

    That said, I would recommend avoiding wchar_t if you care about portability and performance.