Tuesday, July 5, 2011

Using tuple as a key of unordered_map in boost

Yes, this is a very trivial thing, but (surprisingly) none of the sources on web gave me the direct solution. Although the solution is straightforward after all, but it took me tremendous time for me to figure this out, so I would like to leave some memo for future reference.

  1. When you define hash_value(), it is very important that the function is in the same namespace to that of key class.
  2. boost::tuple is in namespace boost::tuples. (Why!?!?)
So you have to include the following code:

typedef tuple param_tuple;

namespace boost {
  namespace tuples {
    std::size_t hash_value(param_tuple const& e) {
      std::size_t seed = 0;
      boost::hash_combine( seed, e.get<0>() );
      boost::hash_combine( seed, e.get<1>() );
      boost::hash_combine( seed, e.get<2>() );
      return seed;
    }
  }
}

No comments:

Post a Comment