In this C++ tutorial, you will learn how to get the size of a tuple, with example programs.
C++ Tuple Size
tuple_size gives access to the number of elements in a given Tuple.
In this tutorial, we will learn how to get the size of a tuple using tuple_size with examples.
Syntax
The syntax of expression using tuple_size, to get the size of tuple x
is
</>
Copy
tuple_size<decltype(x)>::value
Example
In the following program, we declared a Tuple with three elements. We shall get the size of this Tuple programmatically using tuple_size.
C++ Program
</>
Copy
#include <iostream>
#include<tuple>
using namespace std;
int main() {
tuple<int, string, bool> fruit(12, "apple", true);
int size = tuple_size<decltype(fruit)>::value;
cout << "Tuple Size : " << size << endl;
}
Output
Tuple Size : 3
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned how to get the size of a Tuple using tuple_size, with the help of example.