std::rank

定义于头文件 <type_traits>
template< class T >
struct rank;
(C++11 起)

T 是数组类型,则提供等于该数组维数的成员常量 value 。对于任何其他类型, value 为 0 。

目录

辅助变量模板

template< class T >
inline constexpr std::size_t rank_v = rank<T>::value;
(C++17 起)

继承自 std::integral_constant

成员常量

value
[静态]
T 的维数或零
(公开静态成员常量)

成员函数

operator std::size_t
转换对象为 std::size_t ,返回 value
(公开成员函数)
operator()
(C++14)
返回 value
(公开成员函数)

成员类型

类型 定义
value_type std::size_t
type 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 <iostream>
#include <type_traits>
 
int main()
{
    std::cout << std::rank<int[1][2][3]>::value << '\n';
    std::cout << std::rank<int[][2][3][4]>::value << '\n';
    std::cout << std::rank<int>::value << '\n';
}

输出:

3
4
0

参阅

(C++11)
检查类型是否是数组类型
(类模板)
(C++11)
获取数组类型在指定维度的大小
(类模板)
从给定数组类型移除一个维度
(类模板)
移除数组类型所有维度
(类模板)

版本历史

  • (当前 | 先前 2017年11月26日 (日) 09:45Fruderica讨论 | 贡献 . . (1,381字节) (0). . (辅助) (撤销)
  • 当前 | 先前 2017年5月5日 (五) 08:25Fruderica讨论 | 贡献. . (1,381字节) (-46). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 18:18P12bot讨论 | 贡献 . . (1,427字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:54P12bot讨论 | 贡献 . . (1,427字节) (-114). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 14:15P12bot讨论 | 贡献 . . (1,541字节) (+177). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 12:43P12讨论 | 贡献 . . (1,364字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,364字节) (-18). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 04:09P12讨论 | 贡献 . . (1,382字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,382字节) (+1,382). . (Translated from the English version using Google Translate)