简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM