我有多个重载std :: hash operator()的类。比方说:
啊
class A
{
...
}
A.cpp
template<> struct std::hash<A>
{
public:
virtual std::size_t operator()(const A& joint) const noexcept
{
..
}
And similar classes let's say class B
and class C
Now class B
uses A's hash like:
boost::hash_combine(h, std::hash<A>{}(b.getA())
Problem:
error: use of deleted function std::hash<A>::hash()
我试过了: 啊
namespace std
{
template<typename T>
struct hash;
}
h
class B
{
friend struct std::hash<A>;
}
You have to define the
std::hash<A>
specialization inA.h
, so thatB
andC
will be able to see that the specialization exists and that it has anoperator()
member.If
B
andC
can't "see" thestd::hash<A>
specialization, then they will instantiate the primarystd::hash
template, which is disabled because the standard library doesn't know how to hash your user-defined typeA
.So you must define
std::hash<A>
inA.h
. The implementation ofstd::hash<A>::operator()
however, could be in eitherA.h
orA.cpp
.