types_remove_cv

types_remove_cv (타입의 cv 한정자 제거)

이 페이지에서는 C++ 표준 라이브러리의 <type_traits> 헤더에 정의된 remove_cv, remove_const, remove_volatile 템플릿에 대해 설명해요. 이 템플릿들은 주어진 타입에서 최상위 cv 한정자(const, volatile)를 제거한 타입을 멤버 typedef type으로 제공해요. 프로그램에서 이 페이지에 설명된 템플릿에 대해 특수화를 추가하면 동작이 정의되지 않으니 주의해야 해요.

출처: cppreference

본문

정의

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

템플릿 설명 버전
template < class T > struct remove_cv; (1) C++11부터
template < class T > struct remove_const; (2) C++11부터
template < class T > struct remove_volatile; (3) C++11부터

이 템플릿들은 멤버 typedef type을 제공하는데, 이 typeT와 동일하되 최상위 cv 한정자가 제거된 타입이에요.

멤버 타입

이름 정의
type cv 한정자가 없는 타입 T

헬퍼 타입

템플릿 버전
template < class T > using remove_cv_t = typename remove_cv < T >:: type; C++14부터
template < class T > using remove_const_t = typename remove_const < T >:: type; C++14부터
template < class T > using remove_volatile_t = typename remove_volatile < T >:: type; C++14부터

가능한 구현

template < class T > struct remove_cv { typedef T type ; }; template < class T > struct remove_cv < const T > { typedef T type ; }; template < class T > struct remove_cv < volatile T > { typedef T type ; }; template < class T > struct remove_cv < const volatile T > { typedef T type ; }; template < class T > struct remove_const { typedef T type ; }; template < class T > struct remove_const < const T > { typedef T type ; }; template < class T > struct remove_volatile { typedef T type ; }; template < class T > struct remove_volatile < volatile T > { typedef T type ; };

예제

const volatile int *에서 const/volatile을 제거해도 타입은 바뀌지 않아요. 포인터 자체가 const도 아니고 volatile도 아니기 때문이에요.

#include <type_traits>

template<typename U, typename V>
constexpr bool same = std::is_same_v<U, V>;

static_assert
(
    same<std::remove_cv_t<int>, int> &&
    same<std::remove_cv_t<const int>, int> &&
    same<std::remove_cv_t<volatile int>, int> &&
    same<std::remove_cv_t<const volatile int>, int> &&
    // remove_cv only works on types, not on pointers
    not same<std::remove_cv_t<const volatile int*>, int*> &&
    same<std::remove_cv_t<const volatile int*>, const volatile int*> &&
    same<std::remove_cv_t<const int* volatile>, const int*> &&
    same<std::remove_cv_t<int* const volatile>, int*>
);

int main() {}

같이 보기

템플릿 설명
is_const (C++11) 타입이 const 한정자인지 확인해요 (클래스 템플릿)
is_volatile (C++11) 타입이 volatile 한정자인지 확인해요 (클래스 템플릿)
add_cv, add_const, add_volatile (C++11) 주어진 타입에 const 및/또는 volatile 한정자를 추가해요 (클래스 템플릿)
remove_cvref (C++20) std::remove_cvstd::remove_reference를 결합해요 (클래스 템플릿)
remove_cv, remove_const, remove_volatile (C++26) 반영된 타입에서 const 및/또는 volatile 한정자를 제거해요 (함수)

더 알아보기 (Learn more)

cppreference