Namespaces
Variants

std:: rank

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
ヘッダーで定義 <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
[static]
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 <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}};
    // 参照型(例: ary[0])のランク、つまり int(&)[3] は 0:
    static_assert(std::rank_v<decltype(ary[0])> == 0);
    static_assert(std::is_same_v<decltype(ary[0]), int(&)[3]>);
    // 解決策は参照型を除去することです。
    static_assert(std::rank_v<std::remove_cvref_t<decltype(ary[0])>> == 1);
}

関連項目

(C++11)
型が配列型かどうかをチェックする
(クラステンプレート)
(C++11)
指定された次元に沿った配列型のサイズを取得する
(クラステンプレート)
指定された配列型から1つの次元を除去する
(クラステンプレート)
指定された配列型からすべての次元を除去する
(クラステンプレート)