关于std::remove_cv类型特性示例输出正确性的确认请求
确认std::remove_cv示例的输出正确性
先贴出问题里的代码:
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';
示例给出的输出是:
false true
true
你觉得正确输出应该是true false,但实际上示例的输出才是对的,问题出在你搞混了std::remove_cv处理的「顶层cv限定符」和「底层cv限定符」:
先说
type4:const volatile int*说白了就是「指向const volatile int的指针」,这里的const和volatile是管指针指向的那个int的,属于底层cv。而std::remove_cv只负责移除类型本身的顶层cv(也就是直接加在整个类型上的cv修饰),碰不到指针指向对象的cv属性。所以type4的类型还是const volatile int*:std::is_same<type4, int*>::value→ false(类型不一样)std::is_same<type4, const volatile int*>::value→ true(完全匹配)
再看
type5:int* const volatile是「带const volatile修饰的指针,指向int」,这里的cv是直接修饰指针本身的,属于顶层cv。std::remove_cv会把这个顶层修饰去掉,所以type5就是int*,因此std::is_same<type5, int*>::value→ true。
总结:示例的输出没问题,你误解了std::remove_cv的作用范围——它只处理顶层cv,不会动指针指向对象的底层cv。
内容的提问来源于stack exchange,提问作者Vinod
相关产品推荐
相关产品推荐

