0

I am trying to define the following:

  #include <tr1/unordered_set>
  #include <tr1/unordered_map>

  template <class T> class cnf
  {
        public:
        T x;
        bool operator==(const cnf& c) { c.x == x; }
        size_t hash(const cnf& c) { return x.hash(); }
  }

  template <class T> class cnf_table {
    private:
       typedef unordered_set<cnf<T> > cnf_set;

       typedef unordered_map<T, cnf<T> > cnf_map;

           cnf_map my_map;
  }

and I have the following problems:

  1. I am not sure the == and hash will actually be used when I defined this way the unordered_set and unordered_map, will they?

  2. A more severe problem, I don't think this compiles because you can't call x.hash() for an incomplete type T -- is there a different way to define the hash?

  3. When I try to compile something like that I get ton of chain errors, referring to STL include files, finally with something along the lines:

    file.h:67: instantiated from here /usr/include/c++/4.2.1/tr1/hashtable_policy.h:784: error: using invalid field β€˜std::tr1::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal, _H1, H2, std::tr1::_detail::_Default_ranged_hash, false>::_M_h1’

EDIT: About 2, what I am uncertain about is how to use in a template environment the hash value of a type T. I think that for a type T, you can't call a method directly, so I am not sure how to calculate its hash code.

kloop
  • 4,537
  • 13
  • 42
  • 66
  • I don't think it is a duplicate, though it has some similarities. I will edit and make it clear. – kloop Feb 23 '14 at 15:03
  • 2
    It's a duplicate. Just providing a `hash` member is not the correct way to specialize `std::hash`. Also, T is not incomplete. T is a template parameter. At the time you instantiate `cnf` `T` has to be substituted with a complete type. Why use `tr1` in 2014? – pmr Feb 23 '14 at 15:09

0 Answers0