std::experi...::equivalent(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ¦‹ std::experimental::filesystem::equivalent πŸ¦‹

okay so this page lives in the C++ Standard Libary manual, and its page id is std::experi...::equivalent(3) β€” yes, the title is abbreviated like that, iykyk πŸ’…


🌸 NAME 🌸

std::experimental::filesystem::equivalent β€” the same name, doing its little NAME-section echo. We love a consistent queen πŸ‘‘βœ¨


✨ Synopsis ✨

the exact synopsis is giving β€œline breaks are a suggestion,” but here it is verbatim bestie:

Defined in header <experimental/filesystem>
bool equivalent( const path& p1, const path& p2 );
bool equivalent( const path& p1, const path& p2, error_code& ec  (1)
(filesystem TS)
);

okay so this checks whether the paths p1 and p2 refer to the same file or directory and have the same file status as determined by status (symlinks are followed) 🫢

if p1 or p2 does not exist, or if their file type is not file, directory, or symlink (as determined by is_other), an error is reported ⚠️

the non-throwing overload returns false on errors. periodt πŸ’€


πŸŽ€ Parameters πŸŽ€

  • p1, p2 β€” paths to check for equivalence, bestie. that’s the whole vibe check πŸ”
  • ec β€” out-parameter for error reporting in the non-throwing overload. it catches the tea so the function doesn’t throw β˜•πŸ™ˆ

πŸ’– Return value πŸ’–

returns true if p1 and p2 refer to the same file or directory and their file status is the same. returns false otherwise. literally that simple, no cap 🌟


πŸ”₯ Exceptions πŸ”₯

  • the overload that does not take an error_code& parameter throws filesystem_error on underlying OS API errors, constructed with p1 as the first argument, p2 as the second argument, and the OS error code as the error code argument 😭
  • std::bad_alloc may be thrown if memory allocation fails. memory said β€œnot today” πŸ’€
  • the overload taking an error_code& parameter sets it to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur. this overload has noexcept specification: noexcept ✨

🧚 Notes 🧚

two paths are considered to resolve to the same file system entity if st_dev and st_ino of their POSIX stat structure, obtained as if by POSIX stat, are equal β€” meaning, the files are located on the same device at the same location πŸ”—

in particular, all hard links for the same file or directory are equivalent, and a symlink and its target on the same file system are equivalent. iconic behavior honestly 🌈


πŸ“ Example πŸ“

run this code, bestie:

// Run this code

#include <cstdint>
#include <experimental/filesystem>
#include <iostream>
namespace fs = std::experimental::filesystem;

int main()
{
    // hard link equivalency
    fs::path p1 = ".";
    fs::path p2 = fs::current_path();
    if (fs::equivalent(p1, p2))
        std::cout << p1 << " is equivalent to " << p2 << '\n';

    // symlink equivalency
    fs::path p3 = "/lib/libc.so.6";
    fs::path p4 = p3.parent_path() / fs::read_symlink(p3);
    if (fs::equivalent(p3, p4))
        std::cout << p3 << " is equivalent to " << p4 << '\n';
}

🌟 Possible output 🌟

"." is equivalent to "/var/tmp/test"
"/lib/libc.so.6" is equivalent to "/lib/libc-2.12.so"

slay, the paths understood the assignment πŸ’…βœ¨


πŸ‘€ See also πŸ‘€

  • status β€” determines file attributes
  • symlink_status β€” determines file attributes, checking the symlink target (function)

πŸ’œ Categories πŸ’œ

  • Noindexed pages
  • unconditionally noexcept

πŸ™ˆ Hidden categories πŸ™ˆ

  • Pages with unreviewed unconditional noexcept template
  • Pages with unreviewed noexcept template

source/version: http://cppreference.com Β· date: 2024.06.10 Β· page id: std::experi...::equivalent(3) β€” that’s the cute sign-off, bestie πŸ’–πŸŽ€πŸ«Ά

πŸ“¦ where this page comes from