I've this code:
#include<unordered_set>
using namespace std;
struct E{
size_t distance;
bool operator>(const E& e)const{
return distance > e.distance;
}
bool operator<(const E& e)const{
return distance < e.distance;
}
};
int main(){
unordered_set<E> s;
return 0;
}
When I compile it with g++ 7.3 it gives a lot of errors:
g++ m.cpp -std=c++11
In file included from /usr/include/c++/7/bits/hashtable.h:35:0,
from /usr/include/c++/7/unordered_set:47,
from m.cpp:1:
/usr/include/c++/7/bits/hashtable_policy.h: In instantiation of ‘struct std::__detail::__is_noexcept_hash<E, std::hash<E> >’:
/usr/include/c++/7/type_traits:143:12: required from ‘struct std::__and_<std::__is_fast_hash<std::hash<E> >, std::__detail::__is_noexcept_hash<E, std::hash<E> > >’
/usr/include/c++/7/type_traits:154:31: required from ‘struct std::__not_<std::__and_<std::__is_fast_hash<std::hash<E> >, std::__detail::__is_noexcept_hash<E, std::hash<E> > > >’
/usr/include/c++/7/bits/unordered_set.h:98:63: required from ‘class std::unordered_set<E>’
m.cpp:13:22: required from here
/usr/include/c++/7/bits/hashtable_policy.h:87:34: error: no match for call to ‘(const std::hash<E>) (const E&)’
noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/move.h:54:0,
from /usr/include/c++/7/bits/stl_pair.h:59,
from /usr/include/c++/7/utility:70,
from /usr/include/c++/7/unordered_set:38,
from m.cpp:1:
/usr/include/c++/7/type_traits: In instantiation of ‘struct std::__not_<std::__and_<std::__is_fast_hash<std::hash<E> >, std::__detail::__is_noexcept_hash<E, std::hash<E> > > >’:
/usr/include/c++/7/bits/unordered_set.h:98:63: required from ‘class std::unordered_set<E>’
m.cpp:13:22: required from here
/usr/include/c++/7/type_traits:154:31: error: ‘value’ is not a member of ‘std::__and_<std::__is_fast_hash<std::hash<E> >, std::__detail::__is_noexcept_hash<E, std::hash<E> > >’
: public __bool_constant<!bool(_Pp::value)>
Where did I get wrong, how to fix it?
std::unordered_set
does not require operator<
and operator>
for your type E
, which you have provided. It does require a hash function or function-like object (or defaults to std::hash<E>
) which you have not provided, as well as operator==
, which you have also not provided.