Namespaces
Variants
Views
Actions

std::decay

From cppreference.com
< cpp‎ | types
Revision as of 16:50, 1 July 2013 by P12bot (Talk | contribs)

 
 
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Primary type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++11)
(C++14)
Supported operations
Relationships and property queries
(C++11)
(C++11)
(C++11)
(C++11)
Type modifications
(C++11)(C++11)(C++11)
Type transformations
decay
(C++11)
(C++11)
(C++11)
Type trait constants
 
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. This is the type conversion applied to all function arguments when passed by value.

Contents

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

#include <iostream>
#include <type_traits>
 
template <typename T, typename U>
struct decay_equiv : 
    std::integral_constant<
        bool, std::is_same<U, typename std::decay<T>::type
    >::value
> {};
 
int main()
{
    std::cout << std::boolalpha << decay_equiv<int[], int*>::value;
}

Output:

true

See also

implicit conversion array-to-pointer, function-to-pointer, rvalue-to-lvalue conversions