std::filesy...entry::path(3)
β¨ std::filesystem::directory_entry::path β¨
okay bestie, so this is a page straight out of the C++ Standard Libary (their spelling, not mine π
) β it's std::filesystem::directory_entry::path, and honestly it's giving "small but mighty."
πΈ NAME πΈ
std::filesystem::directory_entry::path β yes, the name is the whole thing twice, that's just how the standard library does it, periodt.
π Synopsis π
const std::filesystem::path& path() const noexcept; (since C++17)
operator const std::filesystem::path& () const noexcept; (since C++17)
So you get two ways in β the named path() getter and the conversion operator β both noexcept, both const, both since C++17. Two icons, same energy fr fr. π
What does it do? It returns the full path the directory entry refers to. That's the whole vibe check.
π¦ Parameters π¦
(none) β literally none, it takes zero arguments, it's low-maintenance like that π
β Return value β
The full path the directory entry refers to. Exactly what it says on the tin, no plot twist. π«Ά
π₯ Example π₯
Run this code
#include <filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;
std::string get_stem(const fs::path& p) { return p.stem().string(); }
void create_file(const fs::path& p) { std::ofstream o{p}; }
int main()
{
const fs::path dir{"tmp_dir"};
fs::create_directory(dir);
create_file(dir / "one");
create_file(dir / "two");
create_file(dir / "three");
for (const auto& file : fs::directory_iterator(dir))
{
// Explicit conversion
std::cout << get_stem(file.path()) << '\n';
// Implicit conversion
std::cout << get_stem(file) << '\n';
}
fs::remove_all(dir);
}
Bestie look at this β they call get_stem(file.path()) (explicit conversion) and then just get_stem(file) (implicit conversion), and both work because of that operator const std::filesystem::path&. That's the moment. π«’β¨
π Possible output π
two
two
one
one
three
three
(note the "possible" β it's a directory iterator, the order is not the point, the doubling is the point π)
π See also π
pathβ represents a path (C++17) (class)
okay that's the tea π β this page came from http://cppreference.com, dated 2024.06.10, and you can find it filed under std::filesy...entry::path(3) in the C++ Standard Libary, mwah π
π§π
π¦ 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