I am looking to only make use of hash_combine
API from boost (for the sake of using a key pair within std::unordered_map
) so I copied relevant files to my project (hash.hpp
, hash_mix.hpp
) and only kept seemingly relevant functions uncommented out (shown later below)
However, I seem to be getting the following error upon using it
error: no matching function for call to ‘hash_value(const Bar&)’
Here's how I have it defined
// file.hpp
#include "hash.hpp"
enum class Bar
{
// stuff
};
using faultPair = std::pair<Bar, std::string>;
using keyPair = boost::hash<faultPair>;
std::unordered_map<faultPair, int, keyPair> _map;
Usage
// file.cpp
void foo(Bar bar, std::string str, int value)
{
auto sec = _map.insert({{bar, str}, value}).second; // ERROR: candidate template ignored: could not match 'const std::pair<A, B>' against 'const Bar'
}
Boost lib copied (modified as mentioned above). Copied from here
// hash.hpp
#include "hash_mix.hpp"
// some other headers...
namespace boost
{
template <class T>
inline void hash_combine( std::size_t& seed, T const& v )
{
seed = boost::hash_detail::hash_mix( seed + 0x9e3779b9 + boost::hash<T>()( v ) );
}
template <class T>
struct hash;
template <class T>
std::size_t hash_value( std::complex<T> const& v )
{
std::size_t re = boost::hash<T>()( v.real() );
std::size_t im = boost::hash<T>()( v.imag() );
return re + hash_detail::hash_mix( im );
}
// pair
template <class A, class B>
std::size_t hash_value( std::pair<A, B> const& v )
{
std::size_t seed = 0;
boost::hash_combine( seed, v.first );
boost::hash_combine( seed, v.second );
return seed;
}
// boost::hash
template <class T> struct hash
{
typedef T argument_type;
typedef std::size_t result_type;
std::size_t operator()( T const& val ) const
{
return hash_value( val );
}
};
}
Errors:
1.
seed = boost::hash_detail::hash_mix( seed + 0x9e3779b9 + boost::hash<T>()( v ) );
In instantiation of ‘std::size_t boost::hash::operator()(const T&) const [with T = Bar; std::size_t = long unsigned int]’:
required from ‘void boost::hash_combine(std::size_t&, const T&) [with T = Bar; std::size_t = long unsigned int]’
boost::hash_combine( seed, v.first );
required from ‘std::size_t boost::hash_value(const std::pair<_T1, _T2>&) [with A = Bar; B = std::__cxx11::basic_string; std::size_t = long unsigned int]’
return hash_value( val );
required from ‘std::size_t boost::hash::operator()(const T&) co开发者_开发百科nst [with T = std::pair<Bar, std::__cxx11::basic_string >; std::size_t = long unsigned int]’
return hash_value( val );
error: no matching function for call to ‘hash_value(const std::__cxx11::basic_string&)’
Some compiler notes:
1.
return hash_value( val );
mismatched types ‘const std::complex<_Tp>’ and ‘const Bar’
std::size_t hash_value( std::complex<T> const& v )
template argument deduction/substitution failed:
return hash_value( val );
mismatched types ‘const std::pair<_T1, _T2>’ and ‘const Bar’
精彩评论