Using the template key in std :: unordered_map

I do not understand why my compiler will not accept the code below

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

Compiler command

clang++ -std=c++14 test.cpp -o test

Excerpt from compiler error message

/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&>()))>

Why is it not allowed to use typename M<T>::const_iteratoras a key in std::unordered_map?

+4
source share
1 answer

Because the default template argument for the hash std::unordered_mapis std::hashthat does not provide an implementation for the iterator.

To do this, you need to specify the hash code defined for it, for example

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
    }
};
+5
source

All Articles