Namespaces
Variants

std:: decay

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)
decay
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
헤더에 정의됨 <type_traits>
template < class T >
struct decay ;
(C++11부터)

함수 인자 를 값으로 전달할 때 수행되는 형변환과 동등한 형변환을 수행합니다. 공식적으로:

  • 만약 T 가 " U 의 배열"이거나 이에 대한 참조인 경우, 멤버 typedef type U* 입니다.
  • 그렇지 않고, T 가 함수 타입 F 이거나 이에 대한 참조인 경우, 멤버 typedef type std:: add_pointer < F > :: type 입니다.

프로그램이 std::decay 에 대한 특수화를 추가하는 경우, 그 동작은 정의되지 않습니다.

목차

멤버 타입

이름 정의
type T 에 decay type 변환을 적용한 결과

헬퍼 타입

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

가능한 구현

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;
};

예제

#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() {}

참고 항목

std::remove_cv std::remove_reference 를 결합
(클래스 템플릿)
implicit conversion 배열-포인터, 함수-포인터, lvalue-rvalue 변환