CKTIS / C++
std::filesystem::path: lexical pathname representation
std::filesystem::path stores pathname syntax and elements but does not itself check whether a filesystem object exists or is valid.
Version: C++23
Syntax
class std::filesystem::path {
public:
using value_type = /* operating-system-dependent */;
static constexpr value_type preferred_separator = /* operating-system-dependent */;
bool empty() const noexcept;
bool is_absolute() const;
std::string generic_string() const;
path lexically_normal() const;
path lexically_relative(const path& base) const;
iterator begin() const;
};
path std::filesystem::operator/(const path& lhs, const path& rhs);std::filesystem::path from <filesystem> represents an optional root-name, optional root-directory, and a sequence of filenames. Its value_type, native format, and preferred_separator are platform-dependent; generic_string() methods use the portable / separator.
std::filesystem::path::empty tests for an empty pathname, while std::filesystem::path::is_absolute classifies it under operating-system rules; neither operation queries the filesystem. std::filesystem::path::begin starts iteration over path elements, and std::filesystem::path::generic_string serializes generic format with / separators. std::filesystem::path::lexically_normal normalizes syntax, while std::filesystem::path::lexically_relative computes a purely lexical form relative to a base and can return an empty path for incompatible roots or absolute/relative forms.
The scope spelling std::filesystem::path::operator/ denotes the path-composition obligation, but in C++23 it is not a member name: the actual interface is the non-member std::filesystem::operator/, equivalent to path(lhs) /= rhs. No member alias should be inferred from that spelling.
Operator / composes path elements while respecting root and separator rules, whereas operator+= merely concatenates characters. The example builds logs/2026/run.txt and prints its filename, extension, and logs/2026 parent in generic format.
This is a lexical class: a path can be nonexistent, invalid for the host, or resolve differently relative to a working directory. lexically_normal removes components syntactically and does not follow symlinks. operator== compares lexical representations, not file identity; use filesystem::equivalent on existing objects for the latter.
Examples
#include <filesystem>
#include <iostream>
int main() {
namespace fs = std::filesystem;
const fs::path value = fs::path{"logs"} / "2026" / "run.txt";
std::cout << value.filename().string() << ' '
<< value.extension().string() << ' '
<< value.parent_path().generic_string() << '\n';
}
Result
run.txt .txt logs/2026
std::filesystem::path → run.txt .txt logs/2026