Namespaces
Variants

std:: extent

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, unsigned N = 0 >
struct extent ;
(C++11부터)

만약 T 가 배열 타입인 경우, N [ 0 , std:: rank < T > :: value ) 범위 내에 있을 때 멤버 상수 value 를 배열의 N 번째 차원을 따른 원소 개수로 제공합니다. 다른 모든 타입의 경우, 또는 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 번째 차원을 따른 원소의 개수
(public static member constant)

멤버 함수

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 && // 기본 차원은 0
    std::extent_v<int[3], 0> == 3 && // 위와 동일
    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); // std::size_t로의 암시적 변환과 함께
    const int ints[]{1, 2, 3, 4};
    static_assert(std::extent_v<decltype(ints)> == 4); // 배열 크기
    [[maybe_unused]] int ary[][3] = {{1, 2, 3}};
    // ary[0]은 'int[3]'에 대한 참조 타입이므로, extent를
    // 올바르게 계산할 수 없으며 0을 반환함
    static_assert(std::is_same_v<decltype(ary[0]), int(&)[3]>);
    static_assert(std::extent_v<decltype(ary[0])> == 0);
    // 참조를 제거하면 올바른 extent 값 3을 얻음
    static_assert(std::extent_v<std::remove_cvref_t<decltype(ary[0])>> == 3);
}

참고 항목

(C++11)
타입이 배열 타입인지 확인합니다
(클래스 템플릿)
(C++11)
배열 타입의 차원 수를 구합니다
(클래스 템플릿)
주어진 배열 타입에서 하나의 차원을 제거합니다
(클래스 템플릿)
주어진 배열 타입에서 모든 차원을 제거합니다
(클래스 템플릿)
(C++23)
특정 랭크의 다차원 인덱스 공간에 대한 설명자
(클래스 템플릿)