std::filesy...::hash_value(3)
π std::filesystem::hash_value π
okay bestie, welcome to std::filesystem::hash_value from the C++ Standard Libary manual β section (3), yep, that's the header's spelling, not me π β and this page is literally all about hashing a std::filesystem::path. π
πΈ NAME πΈ
std::filesystem::hash_value β that's the name, bestie. It's the std::filesystem::hash_value function, and she's the main character of this page. π
β¨ Synopsis β¨
Defined in header <filesystem>:
std::size_t hash_value( const std::filesystem::path& p ) noexcept;
(since C++17) β lowkey a classic. π«Ά
π Parameters π
pβ astd::filesystem::pathobject. One parameter, zero drama, iconic behavior. β¨
π Return value π
A hash value such that if for two paths, p1 == p2 then hash_value(p1) == hash_value(p2).
The return value is consistent with std::hash. No cap. π«Ά
π¦ Notes π¦
Equality of two paths is determined by comparing each component separately, so, for example, "a//b" equals "a/b" and has the same hash_value. The way that works is honestly so satisfying. π
hash_value originates from the Boost.filesystem library where it was used for interoperability with boost.hash (which calls hash_value found by argument-dependent lookup or boost::hash_value where available). History! π
π₯ Example π₯
// Run this code
#include <cassert>
#include <cstddef>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <unordered_set>
namespace fs = std::filesystem;
void show_hash(fs::path const& p)
{
std::cout << std::hex << std::uppercase << std::setw(16)
<< fs::hash_value(p) << " : " << p << '\n';
}
int main()
{
auto tmp1 = fs::path{"/tmp"};
auto tmp2 = fs::path{"/tmp/../tmp"};
assert(!(tmp1 == tmp2));
assert(fs::equivalent(tmp1, tmp2));
show_hash(tmp1);
show_hash(tmp2);
for (auto s : {"/a///b", "/a//b", "/a/c", "...", "..", ".", ""})
show_hash(s);
// A hash function object to work with unordered_* containers:
struct PathHash
{
std::size_t operator()(fs::path const& p) const noexcept
{
return fs::hash_value(p);
}
};
std::unordered_set<fs::path, PathHash> dirs{
"/bin", "/bin", "/lib", "/lib", "/opt", "/opt", "/tmp",
"/tmp/../tmp"};
for (fs::path const& p : dirs)
std::cout << p << ' ';
std::cout << '\n';
}
π Possible output: π
6050C47ADB62DFE5 : "/tmp"
62795A58B69AD90A : "/tmp/../tmp"
FF302110C9991974 : "/a///b"
FF302110C9991974 : "/a//b"
FD6167277915D464 : "/a/c"
C42040F82CD8B542 : "..."
D2D30154E0B78BBC : ".."
D18C722215ED0530 : "."
0 : ""
"/tmp/../tmp" "/opt" "/lib" "/tmp" "/bin"
π SEE ALSO π
compareβ compares the lexical representations of two paths lexicographically (public member function).operator==β lexicographically compares two paths (function) β(C++17).operator!=β lexicographically compares two paths (function) β(C++17)(until C++20).operator<β lexicographically compares two paths (function) β(C++17)(until C++20).operator<=β lexicographically compares two paths (function) β(C++17)(until C++20).operator>β lexicographically compares two paths (function) β(C++17)(until C++20).operator>=β lexicographically compares two paths (function) β(C++17)(until C++20).operator<=>β lexicographically compares two paths (function) β(C++20).equivalentβ checks whether two paths refer to the same file system object (function) β(C++17).hashβ hash function object (class template) β(C++11).std::hash<std::filesystem::path>β hash support forstd::filesystem::path(class template specialization) β(C++17).
byeee bestie π«Άπ β this page is from http://cppreference.com, version/date 2024.06.10, and the manual page is std::filesy...::hash_value(3). 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