types_remove_cvref (cv 한정사와 참조 제거)
이 페이지는 C++20에서 도입된 std::remove_cvref 타입 특성에 대해 다뤄요. 주어진 타입에서 참조를 제거하고 최상위 cv 한정사(const, volatile)를 제거한 타입을 얻을 수 있어요. 이를 통해 함수 인자나 템플릿 파라미터에서 타입을 정규화할 때 유용해요.
출처: cppreference
본문
정의
<type_traits> 헤더에 정의됨 |
|
|
template < class T > struct remove_cvref ; |
|
(C++20부터) |
만약 타입 T가 참조 타입이라면, T가 가리키는 타입에서 최상위 cv 한정사를 제거한 타입을 멤버 typedef type으로 제공해요. 그렇지 않으면 T에서 최상위 cv 한정사를 제거한 타입이 type이 돼요.
프로그램이 std::remove_cvref에 대한 특수화를 추가하면 동작이 정의되지 않아요.
멤버 타입
| 이름 |
설명 |
type |
T가 참조라면 T가 가리키는 타입, 참조가 아니라면 T 자신에서 최상위 cv 한정사를 제거한 타입 |
헬퍼 타입
template < class T > using remove_cvref_t = remove_cvref < T >:: type ; |
|
(C++20부터) |
가능한 구현
template < class T > struct remove_cvref { using type = std :: remove_cv_t < std :: remove_reference_t < T >> ; };
참고
| 기능 테스트 매크로 |
값 |
표준 |
기능 |
__cpp_lib_remove_cvref |
201711L |
(C++20) |
std::remove_cvref |
예제
#include <type_traits>
int main()
{
static_assert(std::is_same_v<std::remove_cvref_t<int>, int>);
static_assert(std::is_same_v<std::remove_cvref_t<int&>, int>);
static_assert(std::is_same_v<std::remove_cvref_t<int&&>, int>);
static_assert(std::is_same_v<std::remove_cvref_t<const int&>, int>);
static_assert(std::is_same_v<std::remove_cvref_t<const int[2]>, int[2]>);
static_assert(std::is_same_v<std::remove_cvref_t<const int(&)[2]>, int[2]>);
static_assert(std::is_same_v<std::remove_cvref_t<int(int)>, int(int)>);
}
같이 보기
remove_cv remove_const remove_volatile (C++11) (C++11) (C++11) |
주어진 타입에서 const 및/또는 volatile 한정사를 제거해요 (클래스 템플릿) |
remove_reference (C++11) |
주어진 타입에서 참조를 제거해요 (클래스 템플릿) |
decay (C++11) |
함수 인자를 값으로 전달할 때처럼 타입 변환을 적용해요 (클래스 템플릿) |
remove_cvref (C++26) |
meta::remove_cv와 meta::remove_reference를 결합해요 (함수) |
더 알아보기 (Learn more)
cppreference