Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在std::无序_映射中使用模板键_C++_Templates_C++11_Unordered Map - Fatal编程技术网

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
    }
};