I noticed the "indices trick" being mentioned in the context of pretty-printing tuples. It sounded interesting, so I followed the link.
Well, that did not go well. I understood the question, but could really not follow what was going on. Why do we even need indices of anything? How do the different functions defined there help us? What is 'Bare'? etc.
Can someone give a play-by-play of that thing for the less-than-experts on parameter packs and variadic tuples?
The problem is: we have a
std::tuple<T1, T2, ...>
and we have some functionf
that we can to call on each element, wheref
returns anint
, and we want to store those results in an array.Let's start with a concrete case:
Except writing out all those
get
s is inconvenient and redundant at best, error-prone at worst. Now, let's say we had a typeindex_sequence<0, 1, 2>
. We could use that to collapse that array initialization into a variadic pack expansion:That's because within the function,
f(std::get<Indices>(tuple))...
gets expanded tof(std::get<0>(tuple)), f(std::get<1>(tuple)), f(std::get<2>(tuple))
. Which is exactly what we want.The last detail of the problem is just generating that particular index sequence. C++14 actually gives us such a utility named
make_index_sequence
whereas the article you linked simply explains how one might implement such a metafunction.
Bare
is probably something like, from Luc Danton's answer: