Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 265
Johannes Schaub - litb Avatar asked Mar 13 '13 12:03

Johannes Schaub - litb


1 Answers

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.

like image 53
Andy Prowl Avatar answered Nov 14 '22 05:11

Andy Prowl