Specialize std::formatter for the Path Class
Discover how to specialize the std::formatter for the std::filesystem::path class to enable portable and consistent path display across different platforms. Understand handling of wide and narrow character types and explore filesystem path methods through practical examples.
We'll cover the following...
The path class is used throughout the filesystem library to represent a file or directory path. On POSIX-conformant systems, such as macOS and Linux, the path object uses the char type to represent filenames. On Windows, path uses wchar_t. On Windows, cout and format() will not display primitive strings of wchar_t characters. This means there is no simple out-of-the-box way to write code that uses the filesystem library and is portable across POSIX and Windows.
We could use preprocessor directives to write specific versions of code for Windows. That may be a reasonable solution for some code bases, but for this book, it's messy and does not serve the purpose of simple, portable, reusable recipes. ...