## get<N>(tuple) considered harmful

2017/05/07 2 Comments

I have been toying around with a custom implementation of `tuple`

and came across this pitfall. Suppose you need a tool that would provide a tuple that would be the tail of a given tuple. What’s a tuple? It can be `std::tuple`

or it can be `mytuple`

, so the tool should best be dependent on the tuple’s template template together with the tuple’s type pack:

template<template <class...> class Tuple, class ArgHead, class... ArgTail> auto tuple_tail(Tuple<ArgHead,ArgTail...>& t) -> Tuple<ArgTail...>;

The implementation must construct a tuple with pack expansion involving only the tail arguments. Types are easy, but to get the values from `t`

we need an index sequence going from 1 to `sizeof...(T)-1`

. It is straightforward to get a sequence like that with the use of `std::index_sequence_for`

, but then it starts at 0, that we need to drop. We then send the index sequence together with the tuple `t`

to a helper function that does the actual job. All of that should be fairly straightforward c++ for anybody with a couple of years of experience (did I say a couple? I meant a couple of dozen, of course…):

Read more of this post

## Recent Comments