Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 289
pp2 test2 Avatar asked Mar 20 '26 00:03

pp2 test2


1 Answers

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

like image 93
Remy Lebeau Avatar answered Mar 22 '26 13:03

Remy Lebeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!