Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/container/array/tuple element"

From cppreference.com
< cpp‎ | container‎ | array
m
m (fmt)
 
(8 intermediate revisions by 4 users not shown)
Line 2: Line 2:
 
{{cpp/container/array/navbar}}
 
{{cpp/container/array/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | array}}
+
{{dcl header|array}}
{{dcl | since=c++11 | 1=
+
{{dcl|since=c++11|1=
 
template< std::size_t I, class T, std::size_t N >
 
template< std::size_t I, class T, std::size_t N >
struct tuple_element<I, array<T, N> >;
+
struct tuple_element< I, std::array<T, N> >;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
Provides compile-time indexed access to the type of the elements of the array using tuple-like interface
+
Provides compile-time indexed access to the type of the elements of the array using tuple-like interface.
  
 
===Member types===
 
===Member types===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc hitem | Member type | Definition}}
+
{{dsc hitem|Member type|Definition}}
{{dsc | type | the type of elements of the array}}
+
{{dsc|type|the type of elements of the array}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Possible implementation===
 
===Possible implementation===
 
{{eq fun
 
{{eq fun
| 1=
+
|1=
 
template<std::size_t I, class T>
 
template<std::size_t I, class T>
  struct tuple_element;
+
struct tuple_element;
  
 
template<std::size_t I, class T, std::size_t N>
 
template<std::size_t I, class T, std::size_t N>
  struct tuple_element<I, std::array<T,N> >
+
struct tuple_element<I, std::array<T,N>>
  {
+
{
    using type = T;
+
    using type = T;
  };
+
};
 
}}
 
}}
  
 
===Example===
 
===Example===
{{example |
+
{{example
| code=
+
|code=
 
#include <array>
 
#include <array>
#include <iostream>
 
 
#include <tuple>
 
#include <tuple>
 
#include <type_traits>
 
#include <type_traits>
Line 40: Line 39:
 
int main()
 
int main()
 
{
 
{
  // define compile time constant array and get the type of the element at position 0
+
    // define array and get the type of the element at position 0
  constexpr std::array<int, 10> data {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+
    std::array<int, 10> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  typedef std::tuple_element<0, decltype(data)>::type zero_arr_type;
+
    using T = std::tuple_element<0, decltype(data)>::type; // int
 
+
    static_assert(std::is_same_v<T, int>);
  // define compile time constant tuple and get the types of the elements at positions 0 and 1
+
  constexpr std::tuple<int, double> tup {10, 3.141592};
+
  typedef std::tuple_element<0, decltype(tup)>::type zero_tup_type;
+
  typedef std::tuple_element<1, decltype(tup)>::type one_tup_type;
+
 
+
  std::cout << std::boolalpha;
+
 
+
  // whether the type is constructed from a std::array or not, generated types are equivalent
+
  std::cout << std::is_same<zero_arr_type, zero_tup_type>::value << std::endl;
+
 
+
  // comparing int type to double type, expected to be false
+
  std::cout << std::is_same<zero_arr_type, one_tup_type>::value << std::endl;
+
  
  // in order to compare type directly to an int we must decay the generated type
+
    const auto const_data = data;
  std::cout << std::is_same<std::decay<zero_tup_type>::type, int>::value << std::endl;
+
    using CT = std::tuple_element<0, decltype(const_data)>::type; // const int
  
  // generated type is not exactly an int type!
+
    // the result of tuple_element depends on the cv-qualification of the tuple-like type
  std::cout << std::is_same<zero_tup_type, int>::value << std::endl;
+
    static_assert(!std::is_same_v<T, CT>);
 +
    static_assert(std::is_same_v<CT, const int>);
 
}
 
}
| output=
 
true
 
false
 
true
 
false
 
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/utility/tuple/dsc tuple_element}}
+
{{dsc inc|cpp/language/dsc structured binding}}
{{dsc inc | cpp/utility/pair/dsc tuple_element}}
+
{{dsc inc|cpp/utility/tuple/dsc tuple_element}}
 +
{{dsc inc|cpp/utility/pair/dsc tuple_element}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/container/array/tuple element]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/container/array/tuple element]]
+
[[fr:cpp/container/array/tuple element]]
+
[[it:cpp/container/array/tuple element]]
+
[[ja:cpp/container/array/tuple element]]
+
[[pt:cpp/container/array/tuple element]]
+
[[ru:cpp/container/array/tuple element]]
+
[[zh:cpp/container/array/tuple element]]
+

Latest revision as of 13:47, 6 December 2023

 
 
 
 
Defined in header <array>
template< std::size_t I, class T, std::size_t N >
struct tuple_element< I, std::array<T, N> >;
(since C++11)

Provides compile-time indexed access to the type of the elements of the array using tuple-like interface.

Contents

[edit] Member types

Member type Definition
type the type of elements of the array

[edit] Possible implementation

template<std::size_t I, class T>
struct tuple_element;
 
template<std::size_t I, class T, std::size_t N>
struct tuple_element<I, std::array<T,N>>
{
    using type = T;
};

[edit] Example

#include <array>
#include <tuple>
#include <type_traits>
 
int main()
{
    // define array and get the type of the element at position 0
    std::array<int, 10> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    using T = std::tuple_element<0, decltype(data)>::type; // int
    static_assert(std::is_same_v<T, int>);
 
    const auto const_data = data;
    using CT = std::tuple_element<0, decltype(const_data)>::type; // const int
 
    // the result of tuple_element depends on the cv-qualification of the tuple-like type
    static_assert(!std::is_same_v<T, CT>);
    static_assert(std::is_same_v<CT, const int>);
}

[edit] See also

Structured binding (C++17) binds the specified names to sub-objects or tuple elements of the initializer[edit]
obtains the type of the specified element
(class template specialization) [edit]
obtains the type of the elements of pair
(class template specialization) [edit]