std::rank
From 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 [static] |
T の次元数、またはゼロ(公開静的メンバ定数) |
メンバ関数
| operator std::size_t |
オブジェクトを std::size_t に変換し、value を返します。 (public member function) |
| operator() (C++14) |
value を返します。 (public member function) |
メンバ型
| 型 | 定義 |
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}}; // 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) |
与えられた配列型から1つの次元を削除する (クラステンプレート) |
| (C++11) |
与えられた配列型からすべての次元を削除する (クラステンプレート) |