Code pasted below:
auto myComp = []( std::array<const string, 3> &a, std::array<const string, 3> &b )
{
if ( a[0] == b[0] && a[1] == b[1] && a[2] == b[2] ) {
return true;
} else {
return false;
}
};
auto myHash = []( std::array<const string, 3> &a )
{
return std::hash<std::string>()( a[0] )
^ std::hash<std::string>()( a[1] )
^ std::hash<std::string>()( a[2] );
};
std::unordered_set<std::array<const string, 3>, decltype( myHash ), decltype( myComp )> unordSet( 16, myHash, myComp );
The whole error message is:
MacOSX15.1.sdk/usr/include/c++/v1/__hash_table:607:17 Static assertion failed due to requirement 'integral_constant<bool, false>::value': the specified hash does not meet the Hash requirements
Many thanks in advance.
EDIT:
I managed to find out the definition of the check:
#ifndef _LIBCPP_CXX03_LANG
template <class _Key, class _Hash>
using __check_hash_requirements _LIBCPP_NODEBUG =
integral_constant<bool,
is_copy_constructible<_Hash>::value && is_move_constructible<_Hash>::value &&
__invokable_r<size_t, _Hash, _Key const&>::value >;
Turns out it is this condition that fails the test:
__invokable_r<size_t, _Hash, _Key const&>::value
if (cond) return true; else return false;can — and should — always be replaced byreturn cond;. Don’t write unnecessary boilerplate code._Hashmust accept_Key const&according to this constraint, your hash function doesn't allow const reference. Change argument toconst std::array<const string, 3> &a. Not sure if the standard actually requires that or it's just the compiler invention.__invokable_rsays that_Hashshould be invocable with only one argument being_Key const&and it should returnsize_t. Your function did not allow const ref argument, because it required non-const ref.