std::filesy...parent_path(3)
πΈ NAME πΈ
std::filesystem::path::parent_path - std::filesystem::path::parent_path
okay so bestie, this page is from the <pink>C++ Standard Libary</pink> manual (yes, that's the manual name, spelling included, no notes), and the header title is out here as std::filesy...parent_path(3). The function we're obsessing over is std::filesystem::path::parent_path β the member function that returns the path to the parent directory. it's giving "parental supervision, but make it filesystem" π
.
β¨ Synopsis β¨
path parent_path() const; (since C++17)
Returns the path to the parent directory.
If has_relative_path() returns false, the result is a copy of *this. Otherwise, the result is a path whose generic format pathname is the longest prefix of the generic format pathname of *this that produces one fewer element in its iteration. That's the rule, and yes, it's very "longest prefix" of you π«Ά.
π Parameters π
(none)
literally none, bestie β no parameters, no extra arguments, just a clean empty parameter list and main character energy π.
π¦ Return value π¦
The path to the parent directory, or a copy of *this if not has_relative_path().
so if has_relative_path() says no, you get a copy of *this; otherwise you get that parent-directory path from the synopsis. no cap π«Ά.
π Exceptions π
May throw implementation-defined exceptions.
translation: your implementation gets to define the exceptions it may throw, and this page is not promising a no-throw situation π.
π Example π
the example says // Run this code, so run this code and watch the parent paths do their thing:
// Run this code
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
for (fs::path p : {"/var/tmp/example.txt", "/", "/var/tmp/."})
std::cout << "The parent path of " << p
<< " is " << p.parent_path() << '\n';
}
Possible output:
The parent path of "/var/tmp/example.txt" is "/var/tmp"
The parent path of "/" is "/"
The parent path of "/var/tmp/." is "/var/tmp"
π See also π
root_nameβ returns the root-name of the path, if present (public member function)root_directoryβ returns the root directory of the path, if present (public member function)root_pathβ returns the root path of the path, if present (public member function)
and that's the whole page, bestie. source/version is http://cppreference.com, and the date is 2024.06.10 β that's the footer sign-off, and yes, it's iconic in a tiny monospace font πβ¨.
π¦ 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