如何从数据类型中删除所有 const 限定符?
我尝试使用std::remove_cv但它没有用。
std::remove_cv< const char *>::type
Run Code Online (Sandbox Code Playgroud)
不是一样std::remove_cv<char*>::type吗?
谢谢。
特征是正确地做所有事情:
const char*与 相同char const*,也不相同char* const。所以你的情况,它的指针对象这const,不是指针。并且remove_const(有点合乎逻辑)只删除外部的const,而不是内部的。
如果你真的想删除const指针的ness,你可以这样做:
using T = char const*;
using NoConstT = std::add_pointer<std::remove_cv<std::remove_pointer<T>::type>::type>::type;
Run Code Online (Sandbox Code Playgroud)
(虽然std::add_pointer<T>::type可以放弃支持更简单的T*......)
即:移除指针,移除const指针对象的 ,使结果再次成为指针。
事实上,这是使用R. Martinho Fernandes优秀的Wheels库的好机会,该库为此类嵌套特征提供了便捷的快捷方式:
#include <wheels/meta.h++>
using namespace wheels;
…
using NoConstT = AddPointer<RemoveCv<RemovePointer<T>>>;
Run Code Online (Sandbox Code Playgroud)
更具可读性。