Given a
template<typename First, typename... Tail>
struct something
{
std::tuple<First, Tail...> t;
};
How can I get a std::tuple<Tail...>
that contains all elements from t
except for the first one?
I think this is an interesting question in general, but here is my motivation for context:
I'd like to implement a hash for tuples. I used this answer as a basis. I found that there was an error in it, namely not calling operator()
of the hash object with a value:
return left() ^ right();
Should be:
return left(std::get<0>(e)) ^ right(???);
The ??? would be the remaining elements of the tuple to continue the recursive instantiation of the template. Here is the complete code including the termination part:
#include <functional>
#include <utility>
namespace std
{
template<typename First, typename... Tail>
struct hash<std::tuple<First, Tail...>>
{
typedef size_t result_type;
typedef std::tuple<First, Tail...> argument_type;
result_type operator()(argument_type const& e) const
{
std::hash<First> left;
std::hash<std::tuple<Tail...>> right;
return left(std::get<0>(e)) ^ right(???);
}
};
template<>
struct hash<std::tuple<>>
{
typedef size_t result_type;
typedef std::tuple<> argument_type;
result_type operator()(argument_type const& e) const
{
return 1;
}
};
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…