
Since it carries its value in a template parameter that value is a constant expression that can also be used as a parameter to other templates. The standard library defines the template class integral_constant which encapsulates a static constant of a specified type. Therefore, we cannot use them as template parameters.įortunately, there is an easy way around that problem. to the lambda are ordinary (run-time) values of type size_t. Template parameters can only be constant expressions. The get template takes the index as a template parameter. However, unfortunately, that code will not compile. We have eliminated the need for a specific helper function and can instead rely on one general helper for (hopefully) all cases. Template void function ( Tuple t, index_sequence ) Furthermore, since tuples can have heterogeneous values and C++ is a statically typed language there is no way to dynamically iterate over the values in a generic tuple. It cannot be dynamically generated as e.g. in a for-loop.

The index has to be a constant expression. The standard library function get accepts the index as a template parameter (i.e. at compile time) and returns a reference to the value at that index. The difficulty with tuples in C++ is that they can only be indexed at compile time. Build instructions can be found in the Readme file. If you would like to follow along you can find the code examples on GitHub.
#Tuple unpacking how to
In this post I will discuss how to deal with tuples with very compact code. The C++14 standard introduced a few features that greatly reduce the necessary boilerplate. Unfortunately, tuples can be a little bit tricky to deal with in a generic fashion. As the documentation says they offer a fixed-size collection of heterogeneous values. C++11 introduced tuples to the C++ standard library.
