std::filesy...parent_path(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 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