std::extent
From cppreference.com
| ヘッダ <type_traits> で定義 |
||
| template< class T, unsigned N = 0 > struct extent; |
(C++11以降) | |
T が配列型の場合、N番目の次元に沿った要素数を、メンバー定数 value として提供します。ただし、N が [0, std::rank<T>::value) の範囲内にある場合です。それ以外の型、または T が最初の次元で境界が不明な配列であり、かつ N が 0 の場合、value は 0 になります。
プログラムが std::extent (または std::extent_v (C++17以降))の特殊化を追加した場合、動作は未定義です。
目次 |
[編集] ヘルパー変数テンプレート
| template< class T, unsigned N = 0 > constexpr std::size_t extent_v = extent<T, N>::value; |
(C++17以降) | |
std::integral_constant から継承
メンバ定数
| value [static] |
T の N 番目の次元に沿った要素数(公開静的メンバ定数) |
メンバ関数
| 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, unsigned N = 0> struct extent : std::integral_constant<std::size_t, 0> {}; template<class T> struct extent<T[], 0> : std::integral_constant<std::size_t, 0> {}; template<class T, unsigned N> struct extent<T[], N> : std::extent<T, N - 1> {}; template<class T, std::size_t I> struct extent<T[I], 0> : std::integral_constant<std::size_t, I> {}; template<class T, std::size_t I, unsigned N> struct extent<T[I], N> : std::extent<T, N - 1> {}; |
[編集] 例
このコードを実行
#include <type_traits> static_assert( std::extent_v<int[3]> == 3 && // default dimension is 0 std::extent_v<int[3], 0> == 3 && // the same as above std::extent_v<int[3][4], 0> == 3 && std::extent_v<int[3][4], 1> == 4 && std::extent_v<int[3][4], 2> == 0 && std::extent_v<int[]> == 0 ); int main() { const auto ext = std::extent<int['*']>{}; static_assert(ext == 42); // with implicit conversion to std::size_t const int ints[]{1, 2, 3, 4}; static_assert(std::extent_v<decltype(ints)> == 4); // array size [[maybe_unused]] int ary[][3] = {{1, 2, 3}}; // ary[0] is of type reference to 'int[3]', so, the extent // cannot be calculated correctly and it returns 0 static_assert(std::is_same_v<decltype(ary[0]), int(&)[3]>); static_assert(std::extent_v<decltype(ary[0])> == 0); // removing reference gives correct extent value 3 static_assert(std::extent_v<std::remove_cvref_t<decltype(ary[0])>> == 3); }
[編集] 関連項目
| (C++11) |
型が配列型であるかをチェックする (クラステンプレート) |
| (C++11) |
配列型の次元数を取得する (クラステンプレート) |
| (C++11) |
与えられた配列型から1つの次元を削除する (クラステンプレート) |
| (C++11) |
与えられた配列型からすべての次元を削除する (クラステンプレート) |
| (C++23) |
あるランクの多次元インデックス空間の記述子 (class template) |