types_decay

types_decay (타입 디케이)

std::decay는 함수에 인수를 값으로 전달할 때 수행되는 타입 변환을 그대로 적용하는 클래스 템플릿이에요. 배열은 포인터로, 함수는 함수 포인터로 변환하고, cv 한정자와 참조를 제거해요. 이 페이지에서는 std::decay의 정의, 멤버 타입, 헬퍼 타입, 구현 예시를 확인할 수 있어요.

출처: cppreference

본문

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

헤더 <type_traits>에 정의됨
template < class T > struct decay ; (C++11부터)

std::decay는 함수 인수를 값으로 전달할 때 수행되는 것과 동일한 타입 변환을 수행해요. 공식적으로는 다음과 같아요.

  • T가 "U의 배열" 또는 그에 대한 참조라면, 멤버 typedef typeU*이에요.
  • 그 외에 T가 함수 타입 F 또는 그에 대한 참조라면, typestd::add_pointer<F>::type이에요.
  • 그 외에는 typestd::remove_cv<std::remove_reference<T>::type>::type이에요.

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

멤버 타입 (Member types)

이름 정의
type T에 디케이 타입 변환을 적용한 결과

헬퍼 타입 (Helper types)

template < class T > using decay_t = typename decay < T >:: type ; (C++14부터)

가능한 구현 (Possible implementation)

template < class T > struct decay { private : typedef typename std :: remove_reference < T >:: type U ; public : typedef typename std :: conditional < std :: is_array < U >:: value , typename std :: add_pointer < typename std :: remove_extent < U >:: type >:: type , typename std :: conditional < std :: is_function < U >:: value , typename std :: add_pointer < U >:: type , typename std :: remove_cv < U >:: type >:: type >:: type type ; };

예제 (Example)

#include <type_traits>

template<typename T, typename U>
constexpr bool is_decay_equ = std::is_same_v<std::decay_t<T>, U>;

static_assert
(
    is_decay_equ<int, int> &&
    ! is_decay_equ<int, float> &&
    is_decay_equ<int&, int> &&
    is_decay_equ<int&&, int> &&
    is_decay_equ<const int&, int> &&
    is_decay_equ<int[2], int*> &&
    ! is_decay_equ<int[4][2], int*> &&
    ! is_decay_equ<int[4][2], int**> &&
    is_decay_equ<int[4][2], int(*)[2]> &&
    is_decay_equ<int(int), int(*)(int)>
);

int main() {}

같이 보기 (See also)

암시적 변환 배열-포인터, 함수-포인터, lvalue-rvalue 변환
remove_cvref (C++20) std::remove_cvstd::remove_reference를 결합한 클래스 템플릿
decay (C++26) 함수 인수를 값으로 전달할 때와 같은 타입 변환을 적용하는 함수

더 알아보기 (Learn more)

cppreference