名前空間
変種
操作

std::extent

From cppreference.com
< cpp‎ | types
 
 
メタプログラミングライブラリ
型特性
型のカテゴリ
(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)
extent
(C++11)
型の変更
(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, unsigned N = 0 >
struct extent;
(C++11以降)

T が配列型の場合、N番目の次元に沿った要素数を、メンバー定数 value として提供します。ただし、N[0std::rank<T>::value) の範囲内にある場合です。それ以外の型、または T が最初の次元で境界が不明な配列であり、かつ N0 の場合、value0 になります。

プログラムが 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]
TN 番目の次元に沿った要素数
(公開静的メンバ定数)

メンバ関数

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)
配列型の次元数を取得する
(クラステンプレート) [編集]
与えられた配列型から1つの次元を削除する
(クラステンプレート) [編集]
与えられた配列型からすべての次元を削除する
(クラステンプレート) [編集]
(C++23)
あるランクの多次元インデックス空間の記述子
(class template) [編集]
English 日本語 中文(简体) 中文(繁體)