I have a variable i of type std::size_t and a tuple of type std::tuple. I want to get the i-th element of the tuple. I tried this:
// bindings... is of type const T&...
auto bindings_tuple = std::make_tuple(bindings...);
auto binding = std::tuple_element<i, const T&...>(bindings_tuple);
But I get this compile error saying that the first template argument must be an integral constant expression:
error: non-type template argument of type ‘
std::size_t‘ (aka ‘unsigned long‘) is not an integral constant expression
Is it possible to get the i-th element of a tuple, and how to do that?
I would like to do this without using boost, if possible.
You cannot. That’s not what a tuple is for. If you need dynamic access to an element, use
std::array<T,N>, which is almost identical tostd::tuple<T,...,T>but gives you the dynamic[i]-operator; or even a fully dynamic container likestd::vector<T>.