Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-06-30 18:58:27


Milosz Marian Hulboj wrote:
> I'm looking for advice on hashing the tuples. For my task it seems to be enough to just take every element from the tuple and apply boost::hash_combine to each of the elements. However I am currently thinking of a clever way of writing it in a generic form. Has anyone done something like that and could give some hints?

#include <iostream>
#include <cstddef>

#include <boost/fusion/algorithm/iteration/fold.hpp>
#include <boost/fusion/adapted/boost_tuple.hpp>

#include <boost/functional/hash.hpp>

struct hash_combine {
    typedef std::size_t result_type;
    template<class T>
    std::size_t operator()(const T& arg, std::size_t current) {
        boost::hash_combine(current, arg);

int main() {
    boost::tuple<int, char, std::string> t1(2, 'x', "a string");
    std::size_t hash = boost::fusion::fold(t1, 0, hash_combine());
    std::cout << hash << std::endl;

In Christ,
Steven Watanabe

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at