C++ 在std::无序_映射中使用模板键
我不明白为什么我的编译器不接受下面的代码C++ 在std::无序_映射中使用模板键,c++,templates,c++11,unordered-map,C++,Templates,C++11,Unordered Map,我不明白为什么我的编译器不接受下面的代码 #include <unordered_set> #include <unordered_map> template<class T> using M = std::unordered_set<T>; template<class T> using D = M<T>; template<class T> using DM = std::unordered_map &
#include <unordered_set>
#include <unordered_map>
template<class T>
using M = std::unordered_set<T>;
template<class T>
using D = M<T>;
template<class T>
using DM = std::unordered_map < typename M<T>::const_iterator // Problem
, typename D<T>::const_iterator >; // Problem
int main(int argc, char ** argv)
{
D<int> d;
M<int> m;
DM<int> dm; // Problem
}
编译器错误消息摘录被删除
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.1/../../../../include/c++/5.3.1/bits/hashtable_policy.h:85:11: error:
implicit instantiation of undefined template
'std::hash<std::__detail::_Node_const_iterator<int, true, false> >'
noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
/usr/bin/。/lib/gcc/x86\u 64-linux-gnu/5.3.1/../../../../../../../include/c++/5.3.1/bits/hashtable\u policy.h:85:11:错误:
未定义模板的隐式实例化
'std::hash'
无例外(declval()(declval())>
为什么不允许使用
typename M::const_iterator
作为std::unordered_map
中的键?因为哈希的默认模板参数是,它没有为迭代器提供实现
您需要为它提供使用定义的哈希,例如
struct iterator_hash {
template <typename I>
std::size_t operator()(const I &i) const {
return std::hash<int>()(*i); // or return sth based on i
}
};
struct迭代器\u散列{
模板
std::size\u t运算符()(常数I&I)常数{
return std::hash()(*i);//或基于i返回某物
}
};
此外,我认为不需要这些typename
s,因为M
和D
都不是模板参数。
struct iterator_hash {
template <typename I>
std::size_t operator()(const I &i) const {
return std::hash<int>()(*i); // or return sth based on i
}
};