std::tuple_element
來自 cppreference.com
定義於標頭檔案 <tuple> |
||
定義於標頭檔案 <array> |
||
在標頭檔案 <utility> 中定義 |
||
定義於標頭檔案 <ranges> |
(C++20 起) |
|
定義於標頭檔案 <complex> |
(C++26 起) |
|
template< std::size_t I, class T > struct tuple_element; // 未定義 |
(1) | (C++11 起) |
template< std::size_t I, class T > struct tuple_element< I, const T > { |
(2) | (C++11 起) |
template< std::size_t I, class T > struct tuple_element< I, volatile T > { |
(3) | (C++11 起) (C++20 中已棄用) |
template< std::size_t I, class T > struct tuple_element< I, const volatile T > { |
(4) | (C++11 起) (C++20 中已棄用) |
提供編譯時索引訪問類元組(tuple-like)型別元素的型別。
1) 主模板未定義。需要顯式(完全)或部分特化才能使型別成為類元組。
2-4) cv-限定型別的特化預設簡單地新增相應的cv-限定符。
|
(C++17 起) |
目錄 |
[編輯] 特化
標準庫為標準庫型別提供以下特化
獲取指定元素的型別 (類模板特化) | |
獲取 pair 元素的型別(類模板特化) | |
獲得 array 元素的型別(類模板特化) | |
獲取 std::ranges::subrange 的迭代器或哨兵的型別 (類模板特化) | |
獲取 std::complex 的底層實數和虛數型別 (類模板特化) |
使用者可以為程式定義的型別特化 std::tuple_element
以使其成為類元組。
在通常情況下,當 get
函式返回引用成員或對子物件的引用時,只需要自定義非 cv 限定型別的特化。
[編輯] 成員型別
成員型別 | 定義 |
型別 | 對於標準特化,類元組型別 T 的第 I 個元素的型別,其中 I 在 [ 0, std::tuple_size<T>::value) 範圍內 |
[編輯] 輔助型別
定義於標頭檔案 <tuple> |
||
template< std::size_t I, class T > using tuple_element_t = typename tuple_element<I, T>::type; |
(C++14 起) | |
[編輯] 備註
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_tuple_element_t |
201402L |
(C++14) | std::tuple_element_t
|
[編輯] 示例
執行此程式碼
#include <array> #include <cstddef> #include <iostream> #include <ranges> #include <tuple> #include <type_traits> #include <utility> template<typename T1, typename T2, typename T3> struct Triple { T1 t1; T2 t2; T3 t3; }; // A specialization of std::tuple_element for program-defined type Triple: template<std::size_t I, typename T1, typename T2, typename T3> struct std::tuple_element<I, Triple<T1, T2, T3>> { static_assert(false, "Invalid index"); }; template<typename T1, typename T2, typename T3> struct std::tuple_element<0, Triple<T1, T2, T3>> { using type = T1; }; template<typename T1, typename T2, typename T3> struct std::tuple_element<1, Triple<T1, T2, T3>> { using type = T2; }; template<typename T1, typename T2, typename T3> struct std::tuple_element<2, Triple<T1, T2, T3>> { using type = T3; }; template<typename... Args> struct TripleTypes { static_assert(3 == sizeof...(Args), "Expected exactly 3 type names"); template<std::size_t N> using type = typename std::tuple_element_t<N, Triple<Args...>>; }; int main() { TripleTypes<char, int, float>::type<1> i{42}; std::cout << i << '\n'; using Tri = Triple<int, char, short>; //< Program-defined type static_assert(std::is_same_v<std::tuple_element_t<0, Tri>, int> && std::is_same_v<std::tuple_element_t<1, Tri>, char> && std::is_same_v<std::tuple_element_t<2, Tri>, short>); using Tuple = std::tuple<int, char, short>; static_assert(std::is_same_v<std::tuple_element_t<0, Tuple>, int> && std::is_same_v<std::tuple_element_t<1, Tuple>, char> && std::is_same_v<std::tuple_element_t<2, Tuple>, short>); using Array3 = std::array<int, 3>; static_assert(std::is_same_v<std::tuple_element_t<0, Array3>, int> && std::is_same_v<std::tuple_element_t<1, Array3>, int> && std::is_same_v<std::tuple_element_t<2, Array3>, int>); using Pair = std::pair<Tuple, Tri>; static_assert(std::is_same_v<std::tuple_element_t<0, Pair>, Tuple> && std::is_same_v<std::tuple_element_t<1, Pair>, Tri>); using Sub = std::ranges::subrange<int*, int*>; static_assert(std::is_same_v<std::tuple_element_t<0, Sub>, int*> && std::is_same_v<std::tuple_element_t<1, Sub>, int*>); }
輸出
42
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2212 | C++11 | 某些標頭檔案中不需要 cv 型別的特化,這導致了歧義 | 需要 |
[編輯] 另請參見
結構化繫結 (C++17) | 將指定名稱繫結到初始化器的子物件或元組元素 |
(C++11) |
獲取類元組型別元素的數量 (類模板) |
(C++11) |
透過連線任意數量的 tuple 建立一個 tuple (函式模板) |