std::rank
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T > struct rank; |
(C++11 起) | |
如果 T
是陣列型別,則提供成員常量 value 等於陣列的維數。對於任何其他型別,value 為 0。
如果程式為 std::rank
或 std::rank_v
(C++17 起) 新增特化,則行為未定義。
目錄 |
[編輯] 輔助變數模板
template< class T > constexpr std::size_t rank_v = rank<T>::value; |
(C++17 起) | |
繼承自 std::integral_constant
成員常量
value [靜態] |
T 的維數,或零(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> |
[編輯] 可能的實現
template<class T> struct rank : public std::integral_constant<std::size_t, 0> {}; template<class T> struct rank<T[]> : public std::integral_constant<std::size_t, rank<T>::value + 1> {}; template<class T, std::size_t N> struct rank<T[N]> : public std::integral_constant<std::size_t, rank<T>::value + 1> {}; |
[編輯] 示例
執行此程式碼
#include <type_traits> static_assert(std::rank<int>{} == 0); static_assert(std::rank<int[5]>{} == 1); static_assert(std::rank<int[5][5]>{} == 2); static_assert(std::rank<int[][5][5]>{} == 3); int main() { [[maybe_unused]] int ary[][3] = {{1, 2, 3}}; // The rank of reference type, e.g., ary[0], that is int(&)[3], is 0: static_assert(std::rank_v<decltype(ary[0])> == 0); static_assert(std::is_same_v<decltype(ary[0]), int(&)[3]>); // The solution is to remove the reference type. static_assert(std::rank_v<std::remove_cvref_t<decltype(ary[0])>> == 1); }
[編輯] 參閱
(C++11) |
檢查型別是否為陣列型別 (類模板) |
(C++11) |
獲取陣列型別在指定維度上的大小 (類模板) |
(C++11) |
從給定陣列型別中移除一個維度 (類模板) |
(C++11) |
從給定陣列型別中移除所有維度 (類模板) |