Consider the following code:

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;

This outputs

1
0

That means that std::remove_pointer<const_string_type>::type removed the const qualifier from the type. My understanding is that std::remove_pointer is supposed to produce the exact same type (qualifiers and all) if the type is not a pointer.

Is this correct behavior, or is this possibly a compiler-implementation issue?

有帮助吗?

解决方案

It's a compiler bug. According to the standard (§20.9.7.5, Table 56): "template struct remove_pointer; : If T has type '(possibly cv-qualified) pointer to T1' then the member typedef type shall name T1; otherwise, it shall name T." It should never remove any const or volatile qualifiers from the results.

其他提示

No it does not remove the const (only removes qualifiers from the pointer not the pointed to type), from this link this is a possible implementation.

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;};

The result is actually a bug in the compiler, it should not remove the const of the pointed to type in any situation.

EDIT here is a quote from a table in the standard confirming it. 20.9.7.5 Pointer modifications

template struct remove_pointer;
If T has type “(possibly cv-qualified) pointer to T1” then the member typedef type shall name T1; otherwise, it shall name T.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top