A C++ tuple is a container that can store multiple values of multiple types in it. We can access the elements of the tuple using std::get(), but std::get() always takes a constant variable parameter, so we can not simply iterate through it using a loop. For tasks that require iterating through all elements of the tuple. like printing all elements.
Below is the program to illustrate the iterating over an element tuple:
CPP14
#include <iostream>
#include <string>
#include <tuple>
int main()
{
std::tuple<std::string, std::string,
std::string>
tup( "Geeks" , "for" , "Geeks" );
std::cout << "Values of tuple: " ;
std::cout << std::get<0>(tup)
<< " " << std::get<1>(tup)
<< " " << std::get<2>(tup)
<< std::endl;
tup = std::make_tuple( "Hey" , "Welcome to" ,
"Geeksforgeeks" );
std::cout << "Values of tuple(Modified): " ;
std::cout << std::get<0>(tup) << " "
<< std::get<1>(tup) << " "
<< std::get<2>(tup)
<< std::endl;
return 0;
}
|
Output:
Values of tuple: Geeks for Geeks
Values of tuple(Modified): Hey Welcome to Geeksforgeeks
The problem arises when we try to iterate through the whole tuple. So, we have two methods here, to iterate through the values of a tuple:
- Using Variadic Templates and metaprogramming (No use of std::apply).
- Using Variadic Templates and std::apply.
Using Variadic Templates and Template:
Variadic templates are used to pass multiple arguments packed in one template argument, and that can be expanded later inside the function. Here is how we will go through all elements of a tuple.
Below is the implementation of the same:
CPP
#include <iostream>
#include <string>
#include <tuple>
using namespace std;
template < size_t I = 0, typename ... Ts>
typename enable_if<I == sizeof ...(Ts),
void >::type
printTuple(tuple<Ts...> tup)
{
return ;
}
template < size_t I = 0, typename ... Ts>
typename enable_if<(I < sizeof ...(Ts)),
void >::type
printTuple(tuple<Ts...> tup)
{
cout << get<I>(tup) << " " ;
printTuple<I + 1>(tup);
}
int main()
{
tuple<string, string, string> tup( "Geeks" ,
"for" ,
"Geeks" );
printTuple(tup);
return 0;
}
|
This case is very much simplified using constexpr() function and if constexpr expressions, but that are only available from C++17 onward. I am simplified code for that too, you can run that in C++17.
Below is the implementation of the above approach:
CPP
#include <iostream>
#include <string>
#include <tuple>
using namespace std;
template < size_t I = 0, typename ... Ts>
contexpr void printTuple(tuple<Ts...> tup)
{
if
constexpr(I == sizeof ...(Ts))
{
return ;
}
else {
cout << get<I>(tup) << " " ;
printTuple<I + 1>(tup);
}
}
int main()
{
tuple<string, string, string> tup( "Geeks" ,
"for" ,
"Geeks" );
printTuple(tup);
return 0;
}
|
Output:
Below is the output of the above code:

Explanation:
The requirement of std::get() is a constant index, no variable. We can always specify a constant number for a template function, “I” here is a constant number for the function. So, we will have n+1 instantiations of print_num() function, where n is the size of the tuple, each one has “I” as a constant number for itself. So instantiations of these functions will be like print_tuple, print_tuple, …., and print_tuple, and all these functions will be called in sequence. This is Template Metaprogramming
Note: So, you can not run the above code on Geeksforgeeks IDE, you need to run it on another compiler. If you want to use C++14 or C++11 you can use the first method. Tuples and templates are only available from C++11, so can not use older versions.
Using Variadic Templates and std::apply():
- First, a simple guide on what std::get() is. std::get() implements some function on elements of tuples, considering elements of tuples as values for that function. It takes one function f(x, y, z….) and a tuple (x, y, z…) which are arguments for the function, and returns the value returned by f.
- Now one more thing, about variadic expansion, if we need to apply some function on all values of a variadic template, then we do it like foo(Ts)…, where Ts is our variadic template, and foo() is the function which needs to be applied on all values packed in Ts. Here three dots after function “…” means the function is applied to the expansion of the variadic template.
- Lambda functions are anonymous functions, which can be easily declared and applied. They are implemented like:
[&a, b, c] (int x, float &y) {
// Function Body
}
- Here x and y are arguments to the function where x is passed by values and y by reference. And, x, y, and z are variables that will be used inside the function for some purpose, so they are fed to function, which means they will be available inside the scope of function.
Below is the implementation of the same:
CPP
#include <iostream>
#include <string>
#include <tuple>
template < typename ... Ts>
void printTuple(std::tuple<Ts...> tup)
{
std:: size_t length = sizeof ...(Ts);
std::apply(
[length]( auto const &... ps) {
std::cout << "[ " ;
int k = 0;
((std::cout << ps
<< (++k == length ? "" : "; " )),
...);
std::cout << " ]" ;
},
tuple);
}
int main()
{
std::tuple<std::string,
std::string, std::string>
tup( "Geeks" , "for" , "geeks" );
printTuple(tup);
return 0;
}
|
- Output:
Below is the output of the above code:

Note: std::apply() is only available from C++17. So, you can not run this code on Geeksforgeeks IDE, you need to run it on another compiler. If you want to use C++14 or C++11 you can use the first method. Tuples and templates are only available from C++11, so can not use older versions.