std::tuple_element<std::tuple> (3) - Linux Man Pages

std::tuple_element<std::tuple>: std::tuple_element<std::tuple>

NAME

std::tuple_element<std::tuple> - std::tuple_element<std::tuple>

Synopsis


Defined in header <tuple>
template< std::size_t I, class T > (1) (since C++11)
class tuple_element; /* undefined */
template< std::size_t I, class... Types > (2) (since C++11)
class tuple_element< I, tuple<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< std::size_t I, class T >
class tuple_element< I, const T > {
typedef typename (3) (since C++11)
std::add_const<typename std::tuple_element<I, T>::type>::type type;
};
template< std::size_t I, class T >
class tuple_element< I, volatile T > {
typedef typename (4) (since C++11)
std::add_volatile<typename std::tuple_element<I, T>::type>::type type;
};
template< std::size_t I, class T >
class tuple_element< I, const volatile T > {
typedef typename (5) (since C++11)
std::add_cv<typename std::tuple_element<I, T>::type>::type type;
};


Provides compile-time indexed access to the types of the elements of the tuple.

Member types


Member type Definition
type the type of Ith element of the tuple, where I is in [0, sizeof...(Types))

Helper types


template <std::size_t I, class T> (since C++14)
using tuple_element_t = typename tuple_element<I, T>::type;

Possible implementation


  template< std::size_t I, class T >
  struct tuple_element;


  // recursive case
  template< std::size_t I, class Head, class... Tail >
  struct tuple_element<I, std::tuple<Head, Tail...>>
      : std::tuple_element<I-1, std::tuple<Tail...>> { };


  // base case
  template< class Head, class... Tail >
  struct tuple_element<0, std::tuple<Head, Tail...>> {
     typedef Head type;
  };

Example


// Run this code


  #include <iostream>
  #include <tuple>


  template <class... Args>
  struct type_list
  {
     template <std::size_t N>
     using type = typename std::tuple_element<N, std::tuple<Args...>>::type;
  };


  int main()
  {
     std::cout << std::boolalpha;
     type_list<int, char, bool>::type<2> x = true;
     std::cout << x << '\n';
  }

Output:


  true

See also


                               obtains the type of the elements of array
std::tuple_element<std::array> (class template specialization)


std::tuple_element<std::pair> obtains the type of the elements of pair
                               (class template specialization)
(C++11)