c++ - 为什么 std::remove_pointer 也删除 const 限定符

标签 c++ c++11 typetraits

考虑以下代码:

typedef const std::string const_string_type;
cout << std::is_const<const_string_type>::value << endl;
cout << std::is_const<std::remove_pointer<const_string_type>::type>::value << endl;

这输出

1
0

这意味着std::remove_pointer<const_string_type>::type删除了 const来自类型的限定符。我的理解是 std::remove_pointer如果类型不是指针,则应该产生完全相同的类型(限定符和所有)。

这是正确的行为,还是可能是编译器实现问题?

最佳答案

这是一个编译器错误。根据标准(§20.9.7.5, 表 56):“模板结构 remove_pointer;: 如果 T 的类型为“(可能是 cv 限定的)指向 T1 的指针”,则成员 typedef 类型应命名为 T1;否则,它将命名为 T。”它 不应该从中删除任何 constvolatile 限定符 结果。

关于c++ - 为什么 std::remove_pointer 也删除 const 限定符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18902146/

相关文章:

c++ - auto myFunc() -> int 和 int myFunc() 的区别或好处

c++ - 编写 move 复制和 move 赋值构造函数的有效方法

c++ - 构建一个 vector 以允许未初始化的存储

C++ 模板元编程 : sort list of mutexes to enforce locking order

c++ - 函数调用中的 OpenCV 错误 : insufficient memory,

c++ - 如何从仅提供复制构造函数的类实例化 "empty"对象?

c++ - 无法理解这个可变参数模板示例

c++ - 错误 : expected ‘;’ before ‘generationString’

c++ - std::common_type 实现

c++ - 基于 C++ 标准的定义实现 `is_similar` 类型特征