名稱空間
變體
操作

std::rank

來自 cppreference.com
< cpp‎ | 型別
 
 
超程式設計庫
型別特性
型別類別
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型別屬性
(C++11)
(C++11)
(C++14)
(C++11)(C++26 中已棄用)
(C++11)(直到 C++20*)
(C++11)(C++20 中已棄用)
(C++11)
型別特性常量
元函式
(C++17)
支援的操作
關係與屬性查詢
型別修改
(C++11)(C++11)(C++11)
型別轉換
(C++11)(C++23 中已棄用)
(C++11)(C++23 中已棄用)
(C++11)
(C++11)(直到 C++20*)(C++17)

(C++11)
(C++17)
編譯時有理數算術
編譯時整數序列
 
定義於標頭檔案 <type_traits>
template< class T >
struct rank;
(C++11 起)

如果 T 是陣列型別,則提供成員常量 value 等於陣列的維數。對於任何其他型別,value0

如果程式為 std::rankstd::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)
獲取陣列型別在指定維度上的大小
(類模板) [編輯]
從給定陣列型別中移除一個維度
(類模板) [編輯]
從給定陣列型別中移除所有維度
(類模板) [編輯]