Difference between std::decay and std::remove_cvref?
Asked Answered
R

2

9

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!

Rhodie answered 30/10, 2022 at 23:46 Comment(0)
F
18

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

Festinate answered 31/10, 2022 at 0:2 Comment(1)
I appreciate your answer!Rhodie
P
9

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

Palette answered 31/10, 2022 at 0:6 Comment(1)
I appreciate your answer!Rhodie

© 2022 - 2024 — McMap. All rights reserved.