std::filesy...y_canonical(3)
π NAME π
okay so this is the <cyan>std::filesystem::canonical</cyan> / <cyan>std::filesystem::weakly_canonical</cyan> page from the <pink>C++ Standard Libary</pink> manual (yes, that's how the manual name is spelled in the header, bestie β we keep it π
). The page title is std::filesy...y_canonical(3), and the NAME line is basically std::filesystem::canonical,std::filesystem::weakly_canonical β std::filesystem::canonical,std::filesystem::weakly_canonical. Slay, we're covering both. β¨
β¨ Synopsis β¨
Defined in header <filesystem> β yes, that one, no cap π«Ά
path canonical( const std::filesystem::path& p ); (1) (since C++17)
path canonical( const std::filesystem::path& p, (2) (since C++17)
std::error_code& ec );
path weakly_canonical( const std::filesystem::path& p ); (3) (since C++17)
path weakly_canonical( const std::filesystem::path& p, (4) (since C++17)
std::error_code& ec );
- 1,2) Converts path
pto a canonical absolute path, i.e. an absolute path that has no dot, dot-dot elements or symbolic links in its generic format representation. Ifpis not an absolute path, the function behaves as if it is first made absolute bystd::filesystem::absolute(p). The pathpmust exist. π - 3,4) Returns a path composed by
operator/=from the result of callingcanonical()with a path argument composed of the leading elements ofpthat exist (as determined bystatus(p)orstatus(p, ec)), if any, followed by the elements ofpthat do not exist. The resulting path is in normal form. πΈ
bestie,canonical()requiringpto exist is not a vibe check you can skip β it's literally in the description.weakly_canonical()is the one that handles the non-existent tail. π
π«Ά Parameters π«Ά
pβ a path which may be absolute or relative; forcanonicalit must be an existing pathecβ error code to store error status to
πΈ Return value πΈ
- 1,2) An absolute path that resolves to the same file as
std::filesystem::absolute(p). - 3,4) A normal path of the form
canonical(x)/y, wherexis a path composed of the longest leading sequence of elements inpthat exist, andyis a path composed of the remaining trailing non-existent elements ofp.
π Exceptions π
Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails. π
- 1,3) Throws
std::filesystem::filesystem_erroron underlying OS API errors, constructed withpas the first path argument and the OS error code as the error code argument. - 2,4) Sets a
std::error_code¶meter to the OS API error code if an OS API call fails, and executesec.clear()if no errors occur.
π§ Notes π§
- The function
canonical()is modeled after the POSIXrealpath. iconic behavior, honestly. π - The function
weakly_canonical()was introduced to simplify operational semantics ofrelative().
π Example π
// Run this code
#include <filesystem>
#include <iostream>
int main()
{
/* set up sandbox directories:
a
βββ b
βββ c1
β βββ d <== current path
βββ c2
βββ e
*/
auto old = std::filesystem::current_path();
auto tmp = std::filesystem::temp_directory_path();
std::filesystem::current_path(tmp);
auto d1 = tmp / "a/b/c1/d";
auto d2 = tmp / "a/b/c2/e";
std::filesystem::create_directories(d1);
std::filesystem::create_directories(d2);
std::filesystem::current_path(d1);
auto p1 = std::filesystem::path("../../c2/./e");
auto p2 = std::filesystem::path("../no-such-file");
std::cout << "Current path is "
<< std::filesystem::current_path() << '\n'
<< "Canonical path for " << p1 << " is "
<< std::filesystem::canonical(p1) << '\n'
<< "Weakly canonical path for " << p2 << " is "
<< std::filesystem::weakly_canonical(p2) << '\n';
try
{
[[maybe_unused]] auto x_x = std::filesystem::canonical(p2);
// NOT REACHED
}
catch (const std::exception& ex)
{
std::cout << "Canonical path for " << p2 << " threw exception:\n"
<< ex.what() << '\n';
}
// cleanup
std::filesystem::current_path(old);
const auto count = std::filesystem::remove_all(tmp / "a");
std::cout << "Deleted " << count << " files or directories.\n";
}
π Possible output π
Current path is "/tmp/a/b/c1/d"
Canonical path for "../../c2/./e" is "/tmp/a/b/c2/e"
Weakly canonical path for "../no-such-file" is "/tmp/a/b/c1/no-such-file"
Canonical path for "../no-such-file" threw exception:
filesystem error: in canonical: No such file or directory [../no-such-file] [/tmp/a/b/c1/d]
Deleted 6 files or directories.
π¦ Defect reports π¦
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. π«Ά
- DR:
LWG 2956; Applied to: C++17; Behavior as published:canonicalhas a spurious base parameter; Correct behavior: removed.
π See also π
pathβ represents a path;(C++17);(class)absoluteβ composes an absolute path;(C++17);(function)relativeβ composes a relative path;(C++17);(function)proximateβ(function);(C++17); no extra blurb in the original, so I'm not inventing one π
cute little sign-off bestie: source http://cppreference.com, version/date 2024.06.10, page std::filesy...y_canonical(3) β catch you in the filesystem, 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