types_remove_extent

types_remove_extent (배열의 첫 번째 차원 제거)

이 페이지는 C++ 표준 라이브러리의 std::remove_extent 타입 특성에 대해 설명해요. remove_extent는 주어진 타입이 배열인 경우 첫 번째 차원을 제거한 요소 타입을 제공하고, 배열이 아닌 경우 원래 타입을 그대로 제공해요. 이를 통해 배열 타입에서 요소 타입을 추출하거나 다차원 배열의 차원을 하나씩 줄여나갈 수 있어요.

출처: cppreference

본문

개요

<type_traits> 헤더에 정의되어 있어요.

template < class T > struct remove_extent ;

(C++11부터)

만약 T가 어떤 타입 X의 배열이라면, 멤버 typedef typeX와 같아요. 그렇지 않다면 typeT예요. T가 다차원 배열인 경우 첫 번째 차원만 제거된다는 점에 유의하세요.

프로그램에서 std::remove_extent에 대한 특수화를 추가하면 동작이 정의되지 않아요.

멤버 타입

타입 설명
type T의 요소 타입

헬퍼 타입

template < class T > using remove_extent_t = typename remove_extent < T >:: type ;

(C++14부터)

가능한 구현

template < class T > struct remove_extent { using type = T ; };
template < class T > struct remove_extent < T [] > { using type = T ; };
template < class T , std :: size_t N > struct remove_extent < T [ N ] > { using type = T ; };

예제

#include <algorithm>
#include <iostream>
#include <iterator>
#include <type_traits>

template<class A>
    std::enable_if_t<std::rank_v<A> == 1>
print_1D(const A& a)
{
    std::copy(a, a + std::extent_v<A>,
        std::ostream_iterator<std::remove_extent_t<A>>(std::cout, " "));
    std::cout << '\n';
}

int main()
{
    int a[][3] = {{1, 2, 3}, {4, 5, 6}}; // got a[2][3]
    // print_1D(a); // compile-time error: ‘a’ has rank 2, expected 1
    print_1D(a[1]);
}

출력:

4 5 6

같이 보기

is_array (C++11) 타입이 배열 타입인지 확인해요 (클래스 템플릿)
rank (C++11) 배열 타입의 차원 수를 구해요 (클래스 템플릿)
--- ---
extent (C++11) 지정된 차원에서 배열 타입의 크기를 구해요 (클래스 템플릿)
--- ---
remove_all_extents (C++11) 주어진 배열 타입에서 모든 차원을 제거해요 (클래스 템플릿)
--- ---
remove_extent (C++26) 반영된 배열 타입에서 하나의 차원을 제거해요 (함수)

더 알아보기 (Learn more)

cppreference