考虑以下代码:
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。”它
不应该从中删除任何 const
或 volatile
限定符
结果。
关于c++ - 为什么 std::remove_pointer 也删除 const 限定符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18902146/