std::filesy...::proximate(3)
π std::filesystem::relative, std::filesystem::proximate π
Okay bestie, so this is from the C++ Standard Library and honestly? These two functions are about to become your new faves. They're both in the <filesystem> header, both since C++17, and both are out here doing that path math so you don't have to. Let's get into it β¨
πΈ SYNOPSIS πΈ
So like, this is defined in the header <filesystem>, periodt. Here's the whole lineup of what we're working with:
path relative( const std::filesystem::path& p,
std::error_code& ec );
(1) β since C++17
path relative( const std::filesystem::path& p,
const std::filesystem::path& base =
std::filesystem::current_path() );
(2) β since C++17
path relative( const std::filesystem::path& p,
const std::filesystem::path& base,
std::error_code& ec );
(3) β since C++17
path proximate( const std::filesystem::path& p,
std::error_code& ec );
(4) β since C++17
path proximate( const std::filesystem::path& p,
const std::filesystem::path& base =
std::filesystem::current_path() );
(5) β since C++17
path proximate( const std::filesystem::path& p,
const std::filesystem::path& base,
std::error_code& ec );
(6) β since C++17
β¨ DESCRIPTION β¨
- Okay so this one literally just returns
relative(p, current_path(), ec). Like, that's it. It's thecurrent_path()flavored version. - & 3. These return
pmade relative tobase. And bestie, listen β it resolves symlinks and normalizes bothpandbasebefore any other processing. It's giving thorough. Effectively it returns:
std::filesystem::weakly_canonical(p).lexically_relative(std::filesystem::weakly_canonical(base))
or the error-code flavor:
std::filesystem::weakly_canonical(p, ec).lexically_relative(std::filesystem::weakly_canonical(base, ec))
with one big caveat β the error code form returns path() at the first error occurrence, if any. So like, it bails on the first oopsie π
- This one returns
proximate(p, current_path(), ec). Same energy, different function. - & 6. Effectively returns:
std::filesystem::weakly_canonical(p).lexically_proximate(std::filesystem::weakly_canonical(base))
or:
std::filesystem::weakly_canonical(p, ec).lexically_proximate(std::filesystem::weakly_canonical(base, ec))
And again, the error code form returns path() at the first error occurrence, if any. Consistency icon π
π«Ά PARAMETERS π«Ά
pβ an existing path πbaseβ the base path, against whichpwill be made relative/proximate. It's the reference point, the main character we're measuring from.ecβ error code to store error status to. Like a lil note that says "hey something went wrong, here's what."
π RETURN VALUE π
pmade relative againstcurrent_path().- & 3.
pmade relative againstbase. pmade proximate againstcurrent_path().- & 6.
pmade proximate againstbase.
Simple, clean, slay. β¨
π EXCEPTIONS π
Okay so, any overload not marked noexcept may throw std::bad_alloc if memory allocation fails. Like, sorry, the memory said no π
Overloads 2 and 5 throw std::filesystem::filesystem_error on underlying OS API errors, and it's constructed with:
pas the first path argumentbaseas the second path argument- the OS error code as the error code argument
Overloads 1, 3, 4, and 6 β the β¨error code girliesβ¨ β set a std::error_code& parameter to the OS API error code if an OS API call fails, and execute ec.clear() if no errors occur. Like "all clear, no notes" π
π EXAMPLE π
Run this code, bestie, I promise it's cute:
#include <filesystem>
#include <iostream>
void show(std::filesystem::path x, std::filesystem::path y)
{
std::cout << "x:\t\t " << x << "\ny:\t\t " << y << '\n'
<< "relative(x, y): "
<< std::filesystem::relative(x, y) << '\n'
<< "proximate(x, y): "
<< std::filesystem::proximate(x, y) << "\n\n";
}
int main()
{
show("/a/b/c", "/a/b");
show("/a/c", "/a/b");
show("c", "/a/b");
show("/a/b", "c");
}
π« Possible output π«
x: "/a/b/c"
y: "/a/b"
relative(x, y): "c"
proximate(x, y): "c"
x: "/a/c"
y: "/a/b"
relative(x, y): "../c"
proximate(x, y): "../c"
x: "c"
y: "/a/b"
relative(x, y): ""
proximate(x, y): "c"
x: "/a/b"
y: "c"
relative(x, y): ""
proximate(x, y): "/a/b"
The way the last two cases just give you different answers depending on the function β that's the whole point, and it's iconic. π¦
π SEE ALSO π
pathβ represents a path(C++17)(class)β the OG.absoluteβ composes an absolute path(C++17)(function)β makes things absolute, as one does.canonicalβ composes a canonical path(function)β the fully resolved version of the truth.weakly_canonicalβ(function)(C++17)β canonical but, like, chill about it.lexically_normalβ converts path to normal form β it normalizes, lexically.lexically_relativeβ converts path to relative form β our relative bestie, lexical edition.lexically_proximateβ converts path to proximate form(public member function of std::filesystem::path)β the proximate icon, member-function style.
No cap, these functions are lowkey the backbone of filesystem navigation. If you EVER need to figure out "how do I get from here to there" in C++, this is your girl. β¨
Anyways bestie, that's the page π This was pulled from cppreference.com, version 2024.06.10, and yeahβgo out there and make some paths relative and proximate. You're literally gonna do great. Periodt. ππ«Ά
π¦ 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