std::tuple_size<std::tuple> (3) - Linux Manuals

std::tuple_size<std::tuple>: std::tuple_size<std::tuple>

NAME

std::tuple_size<std::tuple> - std::tuple_size<std::tuple>

Synopsis


Defined in header <tuple>
template< class T > (1) (since C++11)
class tuple_size; /*undefined*/
template< class... Types >
class tuple_size< std::tuple<Types...> > (2) (since C++11)
: public std::integral_constant<std::size_t, sizeof...(Types)> { };
Defined in header <tuple>
Defined in header <array> (since C++17)
Defined in header <utility> (since C++17)
Defined in header <ranges> (since C++20)
Defined in header <span> (since C++20)
template< class T >
class tuple_size<const T> (3) (since C++11)
: public std::integral_constant<std::size_t, tuple_size<T>::value> { };
template< class T >
class tuple_size< volatile T > (4) (since C++11)
: public std::integral_constant<std::size_t, tuple_size<T>::value> { };
template< class T >
class tuple_size< const volatile T > (5) (since C++11)
: public std::integral_constant<std::size_t, tuple_size<T>::value> { };


Provides access to the number of elements in a tuple as a compile-time constant expression.
In addition to being available via inclusion of the <tuple> header, the templates (3-5) are available when either of the headers <array> or <utility> are included.
All specializations of std::tuple_size satisfy UnaryTypeTrait with BaseCharacteristic std::integral_constant<std::size_t, N> for some N.


The cv-T templates (3-5) are SFINAE-friendly: if std::tuple_size<T>::value is ill-formed when treated as an unevaluated operand, (3-5) do not provide the member value. Access checking is performed as if in a context unrelated to tuple_size and T. Only the validity of the immediate context of the expression is considered. This allows


  #include <utility>
  struct X { int a, b; }; (since C++17)
  const auto [x, y] = X(); // decomposition declaration first attempts tuple_size<const X>
                           // which attempts to use tuple_size<X>::value, and that's OK


Helper variable template


template< class T > (since C++17)
inline constexpr std::size_t tuple_size_v = tuple_size<T>::value;


Inherited from std::integral_constant

Member constants


value sizeof...(Types)
         (public static member constant)
[static]

Member functions


                     converts the object to std::size_t, returns value
operator std::size_t (public member function)


operator() returns value
                     (public member function)
(C++14)

Member types


Type Definition
value_type std::size_t
type std::integral_constant<std::size_t, value>

Example


// Run this code


  #include <iostream>
  #include <tuple>


  template <class T>
  void test(T t)
  {
      int a[std::tuple_size<T>::value]; // can be used at compile time


      std::cout << std::tuple_size<T>::value << '\n'; // or at run time
  }


  int main()
  {
      test(std::make_tuple(1, 2, 3.14));
  }

Output:


  3

See also


                            obtains the size of an array
std::tuple_size<std::array> (class template specialization)


std::tuple_size<std::pair> obtains the size of a pair
                            (class template specialization)
(C++11)
                            tuple accesses specified element
std::get(std::tuple) (function template)
                            obtains the type of the specified element
tuple_element (class template specialization)