Channel72
Channel72

Reputation: 24739

Why does std::remove_pointer also remove const qualifier

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?

Upvotes: 2

Views: 970

Answers (2)

aaronman
aaronman

Reputation: 18751

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.

Upvotes: 1

James Kanze
James Kanze

Reputation: 154027

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.

Upvotes: 4

Related Questions