std::filesystem::u8path(3)
π NAME π
okay so bestie, welcome to the std::filesystem::u8path(3) page from the C++ Standard Libary (yes, the header spells it like that, and honestly? iconic). The NAME is literally std::filesystem::u8path β same as the page, same as the function, no notes. It's giving self-titled debut album energy. π
πΈ Synopsis πΈ
Defined in header <filesystem>
template< class Source > (1) (since C++17)
std::filesystem::path u8path( const Source& source );
(deprecated in C++20)
template< class InputIt > (since C++17)
std::filesystem::path u8path( InputIt first, InputIt last (2)
(deprecated in C++20)
);
So it constructs a path p from a UTF-8 encoded sequence of chars or char8_ts (since C++20), supplied either as an std::string, or as std::string_view, or as a null-terminated multibyte string, or as a [first, last) iterator pair. Both overloads are (since C++17) and (deprecated in C++20) β the numbers (1) and (2) are just there to keep track, iykyk.
Here's the vibe check on which path gets taken:
- If
path::value_typeischarand native encoding is UTF-8, constructs a path directly as if bypath(source)orpath(first, last). Note: this is the typical situation of a POSIX system that uses Unicode, such as Linux. Slay for Linux honestly. - Otherwise, if
path::value_typeiswchar_tand native encoding is UTF-16 (this is the situation on Windows), or ifpath::value_typeischar16_t(native encoding guaranteed UTF-16) orchar32_t(native encoding guaranteed UTF-32), then first converts the UTF-8 character sequence to a temporary stringtmpof typepath::string_typeand then the new path is constructed as if bypath(tmp). - Otherwise (for non-UTF-8 narrow character encodings and for non-UTF-16
wchar_t), first converts the UTF-8 character sequence to a temporary UTF-32-encoded stringtmpof typestd::u32string, and then the new path is constructed as if bypath(tmp)(this path is taken on a POSIX system with a non-Unicode multibyte or single-byte encoded filesystem).
π Parameters π
sourceβ a UTF-8 encodedstd::string,std::string_view, a pointer to a null-terminated multibyte string, or an input iterator withcharvalue type that points to a null-terminated multibyte string.first,lastβ pair ofLegacyInputIterators that specify a UTF-8 encoded character sequence.
π§ Type requirements π§
InputItmust meet the requirements ofLegacyInputIterator.- The value type of
SourceorInputItmust becharorchar8_t. (since C++20)
π¦ Return value π¦
The path constructed from the input string after conversion from UTF-8 to the filesystem's native character encoding. That's the whole return value, bestie β no surprises, just a freshly converted path. β¨
π Exceptions π
May throw std::bad_alloc if memory allocation fails.
not the fun kind of allocation fail, okay? it's giving out-of-memory drama. π
π Notes π
On systems where native path format differs from the generic path format (neither Windows nor POSIX systems are examples of such OSes), if the argument to this function is using generic format, it will be converted to native.
β¨ Example β¨
The original page has a // Run this code link, and here's the code in all its glory:
#include <cstdio>
#ifdef _MSC_VER
#include <fcntl.h>
#include <io.h>
#else
#include <clocale>
#include <locale>
#endif
#include <filesystem>
#include <fstream>
int main()
{
#ifdef _MSC_VER
_setmode(_fileno(stderr), _O_WTEXT);
#else
std::setlocale(LC_ALL, "");
std::locale::global(std::locale(""));
#endif
std::filesystem::path p(u8"θ¦γγͺγ.txt");
std::ofstream(p) << "File contents"; // Prior to LWG2676 uses operator string_type()
// on MSVC, where string_type is wstring, only
// works due to non-standard extension.
// Post-LWG2676 uses new fstream constructors
// Native string representation can be used with OS-specific APIs
#ifdef _MSC_VER
if (std::FILE* f = _wfopen(p.c_str(), L"r"))
#else
if (std::FILE* f = std::fopen(p.c_str(), "r"))
#endif
{
for (int ch; (ch = fgetc(f)) != EOF; std::putchar(ch))
{}
std::fclose(f);
}
std::filesystem::remove(p);
}
π Possible output π
File contents
π See also π
pathβ represents a path (C++17) (class)
mwah π this iconic page came from source http://cppreference.com, version/date 2024.06.10, and it's std::filesystem::u8path(3) β bye bestie, go make those paths cute! π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms