251

What are the reasons for the existence of std::decay? In what situations is std::decay useful?

5
  • 6
    It is used in the standard library e.g. when passing arguments to a thread. Those need to be stored, by value, so you cannot store e.g. arrays. Instead, a pointer is stored and so on. It is also a metafunction that mimics the function parameter type adjustments.
    – dyp
    Sep 8, 2014 at 20:25
  • 4
    decay_t<decltype(...)> is a nice combination, to see what auto would deduce. Sep 8, 2014 at 20:37
  • [meta.trans.other] states: This behavior is similar to the [... = several] conversions applied when an lvalue expression is used as an rvalue, but also strips cv-qualifiers from class types in order to more closely model by-value argument passing."
    – dyp
    Sep 8, 2014 at 20:43
  • 12
    std::decay() can do three things. 1 It is able to convert an array of T to T*; 2. It can remove cv qualifier and reference; 3. It converts function T to T*. e.g decay(void(char)) -> void(*)(char). Seems no one mentioned the third usage in the answers.
    – r0n9
    Oct 12, 2017 at 23:48
  • @r0ng It actually does none of these things, since it isn’t a function. Apr 12, 2020 at 17:11

2 Answers 2

256

<joke>It's obviously used to decay radioactive std::atomic types into non-radioactive ones.</joke>

N2609 is the paper that proposed std::decay. The paper explains:

Simply put, decay<T>::type is the identity type-transformation except if T is an array type or a reference to a function type. In those cases the decay<T>::type yields a pointer or a pointer to a function, respectively.

The motivating example is C++03 std::make_pair:

template <class T1, class T2> 
inline pair<T1,T2> make_pair(T1 x, T2 y)
{ 
    return pair<T1,T2>(x, y); 
}

which accepted its parameters by value to make string literals work:

std::pair<std::string, int> p = make_pair("foo", 0);

If it accepted its parameters by reference, then T1 will be deduced as an array type, and then constructing a pair<T1, T2> will be ill-formed.

But obviously this leads to significant inefficiencies. Hence the need for decay, to apply the set of transformations that occurs when pass-by-value occurs, allowing you to get the efficiency of taking the parameters by reference, but still get the type transformations needed for your code to work with string literals, array types, function types and the like:

template <class T1, class T2> 
inline pair< typename decay<T1>::type, typename decay<T2>::type > 
make_pair(T1&& x, T2&& y)
{ 
    return pair< typename decay<T1>::type, 
                 typename decay<T2>::type >(std::forward<T1>(x), 
                                            std::forward<T2>(y)); 
}

Note: this is not the actual C++11 make_pair implementation - the C++11 make_pair also unwraps std::reference_wrappers.

8
  • 2
    "T1 will be deduced as an array type, and then constructing a pair<T1, T2> will be ill-formed." what is the problem here?
    – camino
    May 25, 2016 at 20:36
  • 7
    I get it, in this way we will get pair<char[4],int> which can only accept strings with 4 characters
    – camino
    May 26, 2016 at 15:46
  • @camino I don't get it, are you saying that without std::decay the the first part of the pair would occupy 4 bytes for four chars instead of a one pointer to char? Is that what the std::forward does? Stops it from decaying from an array to a pointer?
    – Zebrafish
    Oct 4, 2017 at 3:30
  • 5
    @Zebrafish It is array decay. For example: template<typename T> void f(T&); f("abc"); T is char(&)[4], but template<typename T> void f(T); f("abc"); T is char*; You can also find an explanation here: stackoverflow.com/questions/7797839/…
    – camino
    Oct 6, 2017 at 0:14
  • 1
    @camino So maybe guaranteed copy elision solves the problem as well, at least in this case, but of course it wasn't available yet. Mar 19, 2021 at 11:39
100

When dealing with template functions that take parameters of a template type, you often have universal parameters. Universal parameters are almost always references of one sort or another. They're also const-volatile qualified. As such, most type traits don't work on them as you'd expect:

template<class T>
void func(T&& param) {
    if (std::is_same<T,int>::value) 
        std::cout << "param is an int\n";
    else 
        std::cout << "param is not an int\n";
}

int main() {
    int three = 3;
    func(three);  //prints "param is not an int"!!!!
}

http://coliru.stacked-crooked.com/a/24476e60bd906bed

The solution here is to use std::decay:

template<class T>
void func(T&& param) {
    if (std::is_same<typename std::decay<T>::type,int>::value) 
        std::cout << "param is an int\n";
    else 
        std::cout << "param is not an int\n";
}

http://coliru.stacked-crooked.com/a/8cbd0119a28a18bd

14
  • 17
    I'm not happy with this. decay is very aggressive, e.g. if applied to a reference to array it yields a pointer. It is typically too aggressive for this kind of metaprogramming IMHO.
    – dyp
    Sep 8, 2014 at 20:28
  • 9
    @SergeRogatch In the case of "universal parameters" / universal references / forwarding references, I'd just remove_const_t< remove_reference_t<T> >, possibly wrapped in a custom metafunction.
    – dyp
    Aug 1, 2017 at 11:11
  • 1
    where is param even being used? It's an argument of func but I don't see it being used anywhere
    – savram
    Sep 17, 2017 at 0:39
  • 2
    @savram: In these pieces of code: it isn't. We're only checking the type, not the value. Everything should work fine if not better if we removed the name of the parameter. Sep 18, 2017 at 17:22
  • 3
    @GabrielStaples yes. According to template, int, const int, int&, const int&, int&&, const int&&, volatile int, volatile int&, volatile int&&, etc are all different. Oct 23, 2020 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.