1

考虑以下代码:

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如果类型不是指针,则应该产生完全相同的类型(限定符和所有)。

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

4

2 回答 2

4

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

于 2013-09-19T18:37:48.183 回答
1

不,它不会删除 const(仅从指针中删除限定符,而不是指向类型),从这个链接来看,这是一个可能的实现。

template< class T > struct remove_pointer                    {typedef T type;};
template< class T > struct remove_pointer<T*>                {typedef T type;};
template< class T > struct remove_pointer<T* const>          {typedef T type;};
template< class T > struct remove_pointer<T* volatile>       {typedef T type;};
template< class T > struct remove_pointer<T* const volatile> {typedef T type;};

结果实际上是编译器中的一个错误,它不应该在任何情况下删除指向类型的 const。

编辑这里是从标准中确认它的表格中的引用。20.9.7.5 Pointer modifications

模板结构remove_pointer;
如果 T 具有类型“(可能是 cv 限定的)指向 T1 的指针”,则成员 typedef 类型应命名为 T1;否则,它应命名为 T。

于 2013-09-19T18:25:02.507 回答