| Member types | ||||||
| Member constants | ||||||
| Member functions | ||||||
| Path decomposition | ||||||
| Non-member functions | ||||||
| ||||||
| Helper classes | ||||||
path& operator/=(const path& p); | (1) | (since C++17) |
template<class Source> path& operator/=(const Source& source); | (2) | (since C++17) |
template<class Source> path& append(const Source& source); | (3) | (since C++17) |
template<class InputIt> path& append( InputIt first, InputIt last); | (4) | (since C++17) |
path::preferred_separator to the generic format of*this.// Where "//host" is a root-namepath("//host")/"foo"// the result is "//host/foo" (appends with separator)path("//host/")/"foo"// the result is also "//host/foo" (appends without separator) // On POSIX,path("foo")/""// the result is "foo/" (appends)path("foo")/"/bar";// the result is "/bar" (replaces) // On Windows,path("foo")/"C:/bar";// the result is "C:/bar" (replaces)path("foo")/"C:";// the result is "C:" (replaces)path("C:")/"";// the result is "C:" (appends, without separator)path("C:foo")/"/bar";// yields "C:/bar" (removes relative path, then appends)path("C:foo")/"C:bar";// yields "C:foo/bar" (appends, omitting p's root-name)
(2) and(3) participate in overload resolution only ifSource andpath are not the same type, and either:
Source is a specialization ofstd::basic_string orstd::basic_string_view, orContents |
| p | - | pathname to append |
| source | - | std::basic_string,std::basic_string_view, null-terminated multicharacter string, or an input iterator pointing to a null-terminated multicharacter sequence, which represents a path name (either in portable or in native format) |
| first, last | - | pair ofLegacyInputIterators that specify a multicharacter sequence that represents a path name |
| Type requirements | ||
-InputIt must meet the requirements ofLegacyInputIterator. | ||
-The value type ofInputIt must be one of the encoded character types (char,wchar_t,char16_t andchar32_t). | ||
*this
May throwstd::bad_alloc if memory allocation fails.
These functions effectively yield an approximation of the meaning of the argument pathp in an environment where*this is the starting directory.
The output is produced on Windows.
#include <filesystem>#include <iostream>namespace fs= std::filesystem; int main(){ fs::path p1="C:"; p1/="Users";// does not insert a separatorstd::cout<<"\"C:\" /\"Users\" == "<< p1<<'\n'; p1/="batman";// inserts fs::path::preferred_separator, '\' on Windowsstd::cout<<"\"C:\" /\"Users\" /\"batman\" == "<< p1<<'\n';}
Possible output:
"C:" / "Users" == "C:Users""C:" / "Users" / "batman" == "C:Users\\batman"
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3244 | C++17 | constraint thatSource cannot bepath was missing | added |
| concatenates two paths without introducing a directory separator (public member function)[edit] | |
(C++17) | concatenates two paths with a directory separator (function)[edit] |