std::filesy...y_relative,(3)
β¨ NAME β¨
okay bestie, welcome to the C++ Standard Libary manual page titled std::filesy...y_relative,(3) π
. the NAME section literally says std::filesystem::path::lexically_normal,std::filesystem::path::lexically_relative, β then a - β then std::filesystem::path::lexically_normal,std::filesystem::path::lexically_relative, again. yes, it repeats itself, and honestly? iconic. std::filesystem::path::lexically_proximate also shows up in the Synopsis, iykyk π¦.
π Synopsis π
no cap, hereβs the exact synopsis:
path lexically_normal() const; (1) (since C++17)
path lexically_relative( const path& base ) const; (2) (since C++17)
path lexically_proximate( const path& base ) const; (3) (since C++17)
- Returns
*thisconverted to normal form in its generic format. - Returns
*thismade relative tobase.- First, if
root_name() != base.root_name()is true oris_absolute() != base.is_absolute()is true or(!has_root_directory() && base.has_root_directory())is true or any filename inrelative_path()orbase.relative_path()can be interpreted as a root-name, returns a default-constructedpath. - Otherwise, first determines the first mismatched element of
*thisandbaseas if byauto [a, b] = mismatch(begin(), end(), base.begin(), base.end()), then- if
a == end()andb == base.end(), returnspath("."), - otherwise, define
Nas the number of nonempty filename elements that are neither dot nor dot-dot in[b, base.end()), minus the number of dot-dot filename elements, IfN < 0, returns a default-constructedpath, - otherwise, if
N = 0anda == end() || a->empty(), returnspath("."), - otherwise returns an object composed from
- a default-constructed
path()followed by Napplications ofoperator/=(path("..")), followed by- one application of
operator/=for each element in the half-open range[a, end()).
- a default-constructed
- if
- First, if
- If the value of
lexically_relative(base)is not an empty path, return it. Otherwise return*this.
πΈ Parameters πΈ
(none) β literally what it says, bestie. no parameters here, periodt π.
π Return value π
- The normal form of the path.
- The relative form of the path.
- The proximate form of the path.
π¨ Exceptions π¨
May throw implementation-defined exceptions.
π§ Notes π§
these conversions are purely lexical, bestie β they do not check that the paths exist, do not follow symlinks, and do not access the filesystem at all. for symlink-following counterparts of lexically_relative and lexically_proximate, see relative and proximate.
- On Windows, the returned path has backslashes (the preferred separators).
- On POSIX, no filename in a relative path is acceptable as a root-name.
π Example π
run this exact code, no cap:
// Run this code
#include <cassert>
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
assert(fs::path("a/./b/..").lexically_normal() == "a/");
assert(fs::path("a/.///b/../").lexically_normal() == "a/");
assert(fs::path("/a/d").lexically_relative("/a/b/c") == "../../d");
assert(fs::path("/a/b/c").lexically_relative("/a/d") == "../b/c");
assert(fs::path("a/b/c").lexically_relative("a") == "b/c");
assert(fs::path("a/b/c").lexically_relative("a/b/c/x/y") ==
"../..");
assert(fs::path("a/b/c").lexically_relative("a/b/c") == ".");
assert(fs::path("a/b").lexically_relative("c/d") == "../../a/b");
assert(fs::path("a/b").lexically_relative("/a/b") == "");
assert(fs::path("a/b").lexically_proximate("/a/b") == "a/b");
}
π©Ή Defect reports π©Ή
the following behavior-changing defect reports were applied retroactively to previously published C++ standards:
- DR:
LWG 3070β Applied to: C++17 β Behavior as published: a filename that can also be a root-name may cause surprising result β Correct behavior: treated as error case. - DR:
LWG 3096β Applied to: C++17 β Behavior as published: trailing"/"and"/."are handled incorrectly β Correct behavior: corrected.
π See also π
relativeβ composes a relative pathproximate(function) (C++17)
thatβs the whole page, bestie π«Ά source: http://cppreference.com; version/date: 2024.06.10; page: std::filesy...y_relative,(3) from the C++ Standard Libary manual. now go slay those paths π
β¨
π¦ 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