std::tuple_size<std::tuple>
來自 cppreference.com
定義於標頭檔案 <tuple> |
||
template< class... Types > struct tuple_size< std::tuple<Types...> > |
(C++11 起) | |
在編譯時將元組中的元素數量作為常量表達式提供。
目錄 |
[編輯] 輔助變數模板
template< class T > constexpr std::size_t tuple_size_v = tuple_size<T>::value; |
(C++17 起) | |
繼承自 std::integral_constant
成員常量
value [靜態] |
sizeof...(Types) (public static 成員常量) |
成員函式
operator std::size_t |
將物件轉換為 std::size_t,返回 value (公開成員函式) |
operator() (C++14) |
返回 value (公開成員函式) |
成員型別
型別 | 定義 |
value_type
|
std::size_t |
型別
|
std::integral_constant<std::size_t, value> |
[編輯] 示例
執行此程式碼
#include <iostream> #include <tuple> template <class T> void test(T value) { int a[std::tuple_size_v<T>]; // can be used at compile time std::cout << std::tuple_size<T>{} << ' ' // or at run time << sizeof a << ' ' << sizeof value << '\n'; } int main() { test(std::make_tuple(1, 2, 3.14)); }
可能的輸出
3 12 16
[編輯] 另請參閱
結構化繫結 (C++17) | 將指定的名稱繫結到初始化器的子物件或元組元素 |
(C++11) |
獲取類元組型別元素的數量 (類模板) |
(C++11) |
獲取 pair 的大小(類模板特化) |
(C++11) |
獲得 array 的大小(類模板特化) |
獲取 std::ranges::subrange 的大小 (類模板特化) | |
(C++11) |
tuple 訪問指定的元素 (函式模板) |