I'm trying to create a class with unordered_set of paths as data member. At first, I declared a class with this member and got error:
Attempting to reference a deleted function
After reading a few posts here as well as reading C++17 path class info, I understood I lacked specifying a hash function. So I tried this:
class A
{
public:
A() = default;
A(const A& fc) = default;
A& operator=(const A& fc) = default;
private:
std::unordered_set<fs::path, std::hash<fs::path>> m_files;
};
but still got the error above despite specifying the built-in hash function. (std::path also have operator= function)
Tried to instantiate an instance of this class, but got the error described. Seem related to the unordered set hash function.
Visual Studio 2019 (v142) 16.11.3 C++ Language standard: ISO C++20 standard (:/std:c++20) C Language standard: Default (Legacy MSVC)
What am I missing here?
Assuming
namespace fs = std::filesystem;is equivalent to
in all ways. Saying
std::hash<fs::path>is the same as not saying it.What you need to do is provide a
std::hash<fs::path>.Personally I wouldn't specialize
std::hashhere, because you don't ownfs::path; to future proof, I'd write my own hasher and pass it in. (the future proofing here is "the standard implements one, and now my code doesn't compile). Last I checked there was a badly worded requirement that your specializations innamespace stdrely on a user defined type (it may have been fixed to be better worded), so it may even be illegal to definestd::hash<fs::path>. I don't much care, because I consider it bad practice even if legal.now:
should compile.