25

Introduced in c++17, std::filesystem::u8path seems to be deprecated in c++20.

What is the reason for this choice? What should I use in c++17? What should I use in c++20?

Guillaume Gris
  • 1,931
  • 17
  • 29
  • 1
    Not sure why it was ever there, it seems like `std::filesystem::path` has a constructor that performs the same as that function. – Fantastic Mr Fox Jan 02 '19 at 09:40
  • 2
    From path page on cppreference : “For portable pathname generation from Unicode strings, see u8path” – Guillaume Gris Jan 02 '19 at 09:45
  • Hmmm, true. But it also says: *If the source character type is char, the encoding of the source is assumed to be the native narrow encoding* (for constructor 6). I am pretty sure this covers unicode where unicode is the native format. It says the same in u8path: *If path::value_type is char and native encoding is UTF-8, constructs a path directly*. Maybe the note is unnecessary and the path constructor does the right thing? – Fantastic Mr Fox Jan 02 '19 at 09:52
  • 1
    Native narrow encoding is UTF-8 on Unix systems, on Windows, it's more complicated according to this question: https://stackoverflow.com/questions/4649388/what-is-the-native-narrow-string-encoding-on-windows – Guillaume Gris Jan 02 '19 at 09:59
  • 2
    Because in C++20 path constructor supports construction from `char8_t` (a new fundamental type for representing UTF-8 encoded values). I've updated https://en.cppreference.com/w/cpp/filesystem/path/path to reflect this. – cpplearner Jan 02 '19 at 10:50
  • 2
    @cpplearner that should be an answer, not a comment – Caleth Jan 02 '19 at 10:54

1 Answers1

20

Because, thanks to the existence of the C++20 feature char8_t, this will work:

path p(u8"A/utf8/path");

u8path existed to allow the detection of the difference between a UTF-8 string and a narrow character string. But since C++20 will give us an actual type for that, it is no longer necessary.


What should I use in c++17?

Use u8path. Deprecation does not mean removed or inaccessible. It merely means subject to eventual removal.

Nicol Bolas
  • 413,367
  • 61
  • 711
  • 904
  • What should I do if I have a UTF-8 encoded `std::string`? – jhasse Apr 23 '20 at 08:51
  • 1
    @jhasse: As stated in the post, "Deprecation does not mean removed or inaccessible." Use `u8path` for now, with the intent that you're eventually going to transition to having a proper UTF-8 encoded `u8string`. – Nicol Bolas Apr 23 '20 at 13:25