Defined in header <type_traits>
template< class T >
struct decay;
(since C++11)

Applies lvalue-to-rvalue, array-to-pointer, and function-to-pointer implicit conversions to the type T, removes cv-qualifiers, and defines the resulting type as the member typedef type. Formally:

  • If T names the type "array of U" or "reference to array of U", the member typedef type is U*.
  • Otherwise, if T is a function type F or a reference thereto, the member typedef type is std::add_pointer<F>::type.
  • Otherwise, the member typedef type is std::remove_cv<std::remove_reference<T>::type>::type.

These conversions model the type conversion applied to all function arguments when passed by value.

The behavior of a program that adds specializations for decay is undefined.

Member types

Name Definition
type the result of applying the decay type conversions to T

Helper types

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

Possible implementation

template< class T >
struct decay {
    typedef typename std::remove_reference<T>::type U;
    typedef typename std::conditional< 
        typename std::remove_extent<U>::type*,
        typename std::conditional< 
            typename std::add_pointer<U>::type,
            typename std::remove_cv<U>::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>;
int main()
        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)>

See also

combines std::remove_cv and std::remove_reference
(class template)
implicit conversion array-to-pointer, function-to-pointer, lvalue-to-rvalue conversions

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.