◐ Shell
clean mode source ↗

std::tuple_element<std::array> - cppreference.com

来自cppreference.com

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

使用 tuple 式接口,提供 array 元素类型的编译时索引访问

成员类型

可能的实现

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

参阅