Home > Software engineering >  Difference between std::decay and std::remove_cvref?
Difference between std::decay and std::remove_cvref?

Time:10-31

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!

CodePudding user response:

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

CodePudding user response:

For non-array and non-function types std::decay and std::remove_cvref are the same. However, for function and array types std::decay behaves differently:

  • It doesn't remove CV or reference qualifiers
  • For arrays it decays (the first dimension)to a pointer
  • For function types, it converts them to function pointers

Read more on cppreference

  • Related