名稱空間
變體
操作

std::tuple_element<std::array>

來自 cppreference.com
< cpp‎ | 容器‎ | array
 
 
 
 
定義於標頭檔案 <array>
template< std::size_t I, class T, std::size_t N >
struct tuple_element< I, std::array<T, N> >;
(C++11 起)

提供編譯時索引訪問陣列元素的型別,使用類似元組的介面。

目錄

[編輯] 成員型別

成員型別 定義
型別 陣列元素的型別

[編輯] 可能實現

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()
{
    // 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>);
}

[編輯] 另請參見

結構化繫結 (C++17) 將指定的名稱繫結到初始化器的子物件或元組元素[編輯]
獲取指定元素的型別
(類模板特化) [編輯]
獲取 pair 元素的型別
(類模板特化) [編輯]