Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507105

Why is pointer_traits not defined for "T* const"?

As seen on http://en.cppreference.com/w/cpp/memory/pointer_traits and related sites (also the boost implementation by boost intrusive), pointer_traits is not specialized for T*const. Why is that?

Upvotes: 12

Views: 401

Answers (1)

Andy Prowl
Andy Prowl

Reputation: 126502

Although this does not qualify as a strong motivation against specifying that a specialization of pointer_traits<> for T* const should exist, I guess an explanation why it was not included could be that pointer_traits<> is mostly meant to be used in a context where template argument deduction (and in particular type deduction) occurs.

Since type deduction disregards top-level cv-qualifications, a specialization for T* const or T* volatile or T* const volatile was probably deemed unnecessary:

#include <type_traits>

template<typename T>
void foo(T)
{
    static_assert(std::is_same<T, int*>::value, "Error"); // Does not fire!
//                                ^^^^
}

int main()
{
    int x = 0;
    int* const p = &x;
    foo(p);
}

Of course this does not mean that having a specialization for T* cv would harm in this scenario, I just meant to provide a possible explanation of why those specializations are missing.

Similarly, no specialization of iterator_traits<> is provided for T* cv.

Upvotes: 8

Related Questions