class template

std::tuple_size

<tuple>
template <class T> class tuple_size;
Tuple size traits
Class designed to access the number of elements in a tuple as a constexpr.

The class is itself undefined for the generic type T, but a specialization for tuple instantiations is defined in the <tuple> header as:
1
2
template <class... Types>
struct tuple_size<tuple<Types...> > : integral_constant<size_t, sizeof...(Types)> {};


A specialization for this class template also exists for the tuple-like types array and pair in their respective headers, also inheriting from integral_constant with member value defined to the appropriate constexpr value.

Template parameters

T
Type for which the tuple size is to be obtained.
This shall be a class for which a specialization of this class exists, such as a tuple, and tuple-like classes array and pair.

Member types

member typedefinition
valueThe number of elements in the tuple or tuple-like object.
This is a constexpr value of the unsigned integral type size_t.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// tuple_size
#include <iostream>
#include <tuple>

int main ()
{
  std::tuple<int,char,double> mytuple (10,'a',3.14);

  std::cout << "mytuple has ";
  std::cout << std::tuple_size<decltype(mytuple)>::value;
  std::cout << " elements." << std::endl;

  return 0;
}


Output:
mytuple has 3 elements

See also