template.hpp 826 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright 2012 Daniel James.
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. // This is an example of how to write a hash function for a template
  5. // class.
  6. #include <boost/container_hash/hash_fwd.hpp>
  7. template <typename A, typename B>
  8. class my_pair
  9. {
  10. A value1;
  11. B value2;
  12. public:
  13. my_pair(A const& v1, B const& v2)
  14. : value1(v1), value2(v2)
  15. {}
  16. bool operator==(my_pair const& other) const
  17. {
  18. return value1 == other.value1 &&
  19. value2 == other.value2;
  20. }
  21. friend std::size_t hash_value(my_pair const& p)
  22. {
  23. std::size_t seed = 0;
  24. boost::hash_combine(seed, p.value1);
  25. boost::hash_combine(seed, p.value2);
  26. return seed;
  27. }
  28. };