Search code examples
c++metaprogramming

Difference between std::decay and std::remove_cvref?


Does std::remove_cvref replace std::decay after C++20?

From this link, I cannot understand what this means:

C++20 will have a new trait std::remove_cvref that doesn't have undesirable effect of std::decay on arrays

What is the undesirable effect of std::decay?

Example and explanation, please!


Solution

  • std::remove_cvref does not replace std::decay. They are used for two different things.

    An array naturally decays into a pointer to its first element. std::decay will decay an array type to a pointer type. So, for example, std::decay<const char[N]>::type is const char*.

    Whereas std::remove_cvref removes const, volatile and & from a type without changing anything else about the type. So, for example, std::remove_cvref<const char[N]>::type is char[N] rather than char*.