Skip to content

Search topics & cppreference

Search all study topics, concept maps, and 6,587 C++ and C reference pages.

std::decay

Performs the type conversions equivalent to the ones performed when passing function arguments by value. Formally:

Type support
  • C++11
  • C++14
  • C++20
1 min readView on cppreference
NotebookLM
Defined in header <type_traits>
template< class T >
struct decay;
(since C++11)

Performs the type conversions equivalent to the ones performed when passing function arguments by value. Formally:

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

If the program adds specializations for std::decay, the behavior 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

snippet.cpp
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

example.cpp
#include <type_traits>
 
template<typename T, typename U>
constexpr bool is_decay_equ = std::is_same_v<std::decay_t<T>, U>;
 
int main()
{
    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)>
    );
}

See also

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

Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.