0

In C++17 we will have file system support. This example from cppreference is supposed to work correctly:

fs::path p = fs::u8path(u8"要らない.txt");

// native string representation can be used with OS APIs
std::ofstream(p) << "File contents"; // this uses operator string()

However, I don't see how it will work on Windows, since std::filesystem::path::operator string_type() will return std::wstring on Windows, and std::basic_ofstream constructor accepts only std::string. Is this right? I hope not, thus where is my mistake?

Mikhail
  • 20,685
  • 7
  • 70
  • 146
  • Updated that example for Windows compat. They actually have an overload for ofstream ctor that takes a wstring and it works for me. It's an MSVC extension though, so good question – Cubbi Oct 25 '16 at 17:16
  • So this behavior effectively relies on non-standard constructor documented only for one compiler?.. – Mikhail Oct 25 '16 at 18:31
  • 2
    By the time C++17 is actually published, `[io]?fstream` [will have the right constructors](http://wg21.link/LWG2676). – T.C. Oct 25 '16 at 19:05
  • @T.C. great, thanks, this should be an (accepted) answer! BTW, could you please give a hint on how could I have found this information myself? – Mikhail Oct 25 '16 at 19:13
  • @Cubbi Is this ok that cppreference example contains Microsoft-specific code? – Mikhail Oct 25 '16 at 19:16
  • _wfopen is an example of "OS API" that would consume a native representation. The I/O.. I'm not sure, maybe it can be left standard in hope for yet another prod to MS to support Unicode. Either way, this would be best on the discussion tab of that cppreference page. – Cubbi Oct 25 '16 at 19:27
  • @Cubbi Commented on discussion page. – Mikhail Oct 25 '16 at 19:35
  • 1
    @Mikhail Well, I follow the LWG issue list fairly closely (and [one of the issues I submitted](http://wg21.link/LWG2711) has an example that depends on this issue), so I didn't need to look for anything other than the exact issue number... – T.C. Oct 25 '16 at 20:04
  • 1
    @T.C. Thanks! Unfortunately, this recipe does not work for everyone :) – Mikhail Oct 25 '16 at 20:31

0 Answers0