std::filesy...::path::path(3)
π std::filesystem::path::path β the one and only
okay so this is std::filesystem::path::path(3), straight out of the C++ Standard Libary manual, and bestie, it's a lot β but it's literally just "how do I make a path object" and honestly? iconic. every single one of these is (since C++17), no cap. let's get into it πΈ
β¨ Synopsis β¨
eight overloads. eight. and yes you're gonna learn all of them π
path() noexcept;
(1) (since C++17)
path( const path& p );
(2) (since C++17)
path( path&& p ) noexcept;
(3) (since C++17)
path( string_type&& source, format fmt = auto_format );
(4) (since C++17)
template< class Source >
(5) (since C++17)
path( const Source& source, format fmt = auto_format );
template< class InputIt >
(6) (since C++17)
path( InputIt first, InputIt last, format fmt = auto_format );
template< class Source >
path( const Source& source, const std::locale& loc, format fmt =
(7) (since C++17)
auto_format );
template< class InputIt >
path( InputIt first, InputIt last, const std::locale& loc, format
(8) (since C++17)
fmt = auto_format );
so what does this whole thing do? it constructs a new path object. that's the vibe π¦
- Constructs an empty path. blank. empty. nothing. a clean slate β¨
- Copy constructor. Constructs a
pathwhose pathname β in both native and generic formats β is the same as that ofp. twin energy π― - Move constructor. Constructs a
pathwhose pathname, in both native and generic formats, is the same as that ofp, andpis left in a valid but unspecified state. it's giving "no thoughts head empty" but like, legally π - 4β6) Constructs the path from a character sequence (format interpreted as specified by
fmt) provided by source (4, 5), which is a pointer or an input iterator to a null-terminated character/wide character sequence, anstd::basic_stringor anstd::basic_string_view, or represented as a pair of input iterators[first, last)(6). Any of the character typeschar,char8_t(since C++20),char16_t,char32_t,wchar_tis allowed, and the method of conversion to the native character set depends on the character type used by source. π§- If the source character type is
char, the encoding of the source is assumed to be the native narrow encoding (so no conversion takes place on POSIX systems). - If the source character type is
char8_t(since C++20), conversion from UTF-8 to native filesystem encoding is used. - If the source character type is
char16_t, conversion from UTF-16 to native filesystem encoding is used. - If the source character type is
char32_t, conversion from UTF-32 to native filesystem encoding is used. - If the source character type is
wchar_t, the input is assumed to be the native wide encoding (so no conversion takes places on Windows). π
- If the source character type is
- 7, 8) Constructs the path from a character sequence (format interpreted as specified by
fmt) provided by source (7), which is a pointer or an input iterator to a null-terminated character sequence, anstd::string, anstd::string_view, or represented as a pair of input iterators[first, last)(8). The only character type allowed ischar. Useslocto perform the character encoding conversion. Ifvalue_typeiswchar_t, converts from to wide using thestd::codecvt<wchar_t, char, std::mbstate_t>facet ofloc. Otherwise, first converts to wide using thestd::codecvt<wchar_t, char, std::mbstate_t>facet and then converts to filesystem native character type usingstd::codecvt<wchar_t,value_type>facet ofloc. π (yes that sentence is a marathon, i don't make the rules)
(5) and (7) participate in overload resolution only ifSourceandpathare not the same type, and either: -Sourceis a specialization ofstd::basic_stringorstd::basic_string_view, or -std::iterator_traits<std::decay_t<Source>>::value_typeis valid and denotes a possibly const-qualified encoding character type (char,char8_t(since C++20),char16_t,char32_t, orwchar_t).
π Parameters π
pβ a path to copysourceβstd::basic_string,std::basic_string_view, pointer to a null-terminated character string, or input iterator with a character value type that points to a null-terminated character sequence (the character type must becharfor overload (7))first,lastβ pair of LegacyInputIterators that specify a character sequencefmtβ enumerator of typepath::formatwhich specifies how pathname format is to be interpretedlocβ locale that defines encoding conversion to use
π Type requirements π
InputItmust meet the requirements of LegacyInputIterator.- The value type of
InputItmust be one of the character typeschar,wchar_t,char8_t(since C++20),char16_tandchar32_tto use the overload (6). - The value type of
InputItmust becharto use the overload (8).
π₯ Exceptions π₯
2, 4β8) May throw implementation-defined exceptions. (not me warning you twice π)
π Notes π
For portable pathname generation from Unicode strings, see u8path. (until C++20)
(since C++20) the path constructor supports creation from UTF-8 string when the source is a sequence of char8_t. slay β¨
π Example π
// Run this code
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
fs::path p1 = "/usr/lib/sendmail.cf"; // portable format
fs::path p2 = "C:\\users\\abcdef\\AppData\\Local\\Temp\\"; // native format
fs::path p3 = U"D:/η«.txt"; // UTF-32 string
fs::path p4 = u8"~/η.txt"; // UTF-8 string
std::cout << "p1 = " << p1 << '\n'
<< "p2 = " << p2 << '\n'
<< "p3 = " << p3 << '\n'
<< "p4 = " << p4 << '\n';
}
β¨ Output β¨
p1 = "/usr/lib/sendmail.cf"
p2 = "C:\\users\\abcdef\\AppData\\Local\\Temp\\"
p3 = "D:/η«.txt"
p4 = "~/η.txt"
look at those paths go ππ«Ά so clean.
π¦ Defect reports π¦
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. (yes, they literally went back in time to fix stuff β main character energy π)
- LWG 3244 β applied to C++17 β behavior as published: constraint that
Sourcecannot bepathwas missing β correct behavior: constraint thatSourcecannot bepathwas added β
π« See also π«
u8pathβ creates a path from a UTF-8 encoded source (C++17) (deprecated in C++20) (function)
and that's the whole page, bestie β you ate that ππ
source + version: http://cppreference.com, dated 2024.06.10 β go forth and construct paths, ily π
π¦ 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