Search code examples
c++11type-traits

A question about type traits std::remove_cv


My reference is to the example provided hereunder:

std::remove_cv, std::remove_const, std::remove_volatile

In the example,

using type4 = std::remove_cv<const volatile int*>::type;
using type5 = std::remove_cv<int* const volatile>::type;

std::cout << std::is_same<type4, int*>::value << ' '
              << std::is_same<type4, const volatile int*>::value << '\n';

    std::cout << std::is_same<type5, int*>::value << '\n';

Output

false true

true

I am assuming that there is a typo in the output as it doesn't match the test in the example, if my understanding of the concept is correct. The output instead should have been

true false

true

Can someone confirm or correct this?

TIA


Solution

  • In const volatile int *, const and volatile refer to int, not the pointer. remove_cv affects the qualifiers of the type itself, which is a pointer without any qualifiers in your case. So the displayed behavior is correct. If you want const and volatile to affect the pointer rather than the pointed-to type, the construct you used for type5 is correct. If you want remove_cv to affect the pointed-to type, you'll have to remove the pointer and add it back in manually