SysTutorials

Linux Manual Pages › Section 3

stddecay(3)

std::decay - std::decay

Synopsis

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

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:

Member types

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

Helper types

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

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::remove_extent<U>::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

// Run this code

#include <iostream>
#include <type_traits>
template <typename T, typename U>
struct decay_equiv : std::is_same<typename std::decay<T>::type, U>::type
{
};
int main()
{
std::cout << std::boolalpha
<< decay_equiv<int, int>::value << ‘\n’
<< decay_equiv<int &, int>::value << ‘\n’
<< decay_equiv<int &&, int>::value << ‘\n’
<< decay_equiv<const int &, int>::value << ‘\n’
<< decay_equiv<int[2], int >::value << ‘\n’
<< decay_equiv<int(int), int ()(int)>::value << ‘\n’;
}

Output:

  true   true   true   true   true   true

See also

remove_cvref combines std::remove_cv and std::remove_reference                     (class template) (C++20) implicit_conversion array-to-pointer, function-to-pointer, lvalue-to-rvalue conversions