Difference between revisions of "cpp/container/array/tuple element"
From cppreference.com
m (fix misleading comment - that array is not `constexpr` anymore (but it was in 2017 :') |
m (→See also: +) |
||
Line 62: | Line 62: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
+ | {{dsc inc | cpp/language/dsc structured binding}} | ||
{{dsc inc | cpp/utility/tuple/dsc tuple_element}} | {{dsc inc | cpp/utility/tuple/dsc tuple_element}} | ||
{{dsc inc | cpp/utility/pair/dsc tuple_element}} | {{dsc inc | cpp/utility/pair/dsc tuple_element}} |
Revision as of 02:21, 25 March 2021
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 |
Member types
Member type | Definition |
type | the type of elements of the array |
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; }; |
Example
Run this code
#include <array> #include <iostream> #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 std::cout << std::boolalpha; std::cout << std::is_same<T, int>::value << '\n'; 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 std::cout << std::is_same<T, CT>::value << '\n'; std::cout << std::is_same<CT, const int>::value << '\n'; }
Output:
true false true
See also
Structured binding (C++17) | binds the specified names to sub-objects or tuple elements of the initializer |
obtains the type of the specified element (class template specialization) | |
obtains the type of the elements of pair (class template specialization) |