Namespaces
Variants

std:: tuple_element <std::array>

From cppreference.net
定义于头文件 <array>
template < std:: size_t I, class T, std:: size_t N >
struct tuple_element < I, std:: array < T, N > > ;
(C++11 起)

通过类元组接口提供对数组元素类型的编译时索引访问。

目录

成员类型

成员类型 定义
type 数组元素的类型

可能的实现

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;
};

示例

#include <array>
#include <tuple>
#include <type_traits>
int main()
{
    // 定义数组并获取位置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
    // tuple_element的结果取决于元组类类型的cv限定符
    static_assert(!std::is_same_v<T, CT>);
    static_assert(std::is_same_v<CT, const int>);
}

参见

结构化绑定 (C++17) 将指定名称绑定到初始化器的子对象或元组元素
获取指定元素的类型
(类模板特化)
获取 pair 元素的类型
(类模板特化)