fork(2) download
  1. #include <tuple>
  2. #include <cstring>
  3. #include <functional>
  4. #include <type_traits>
  5.  
  6. size_t hash_combiner(size_t left, size_t right) //replacable
  7. { return left^right;}
  8.  
  9. template<int index, class...types>
  10. struct hash_impl {
  11. size_t operator()(size_t a, const std::tuple<types...>& t) const {
  12. typedef typename std::tuple_element<index, std::tuple<types...>>::type nexttype;
  13. hash_impl<index-1, types...> next;
  14. size_t b = std::hash<nexttype>()(std::get<index>(t));
  15. return next(hash_combiner(a, b), t);
  16. }
  17. };
  18. template<class...types>
  19. struct hash_impl<0, types...> {
  20. size_t operator()(size_t a, const std::tuple<types...>& t) const {
  21. typedef typename std::tuple_element<0, std::tuple<types...>>::type nexttype;
  22. size_t b = std::hash<nexttype>()(std::get<0>(t));
  23. return hash_combiner(a, b);
  24. }
  25. };
  26.  
  27. namespace std {
  28. template<class...types>
  29. struct hash<std::tuple<types...>> {
  30. size_t operator()(const std::tuple<types...>& t) {
  31. const size_t begin = std::tuple_size<std::tuple<types...>>::value-1;
  32. return hash_impl<begin, types...>()(1, t); //1 should be some largervalue
  33. }
  34. };
  35. }
  36.  
  37. #include <iostream>
  38.  
  39. int main() {
  40. typedef std::tuple<int, char, float> T;
  41. T t(14, 'A', 3.14);
  42. std::cout << std::hash<T>()(t);
  43. return 0;
  44. }
Success #stdin #stdout 0s 2828KB
stdin
Standard input is empty
stdout
488828571