[英]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
. 正如在http://en.cppreference.com/w/cpp/memory/pointer_traits和相关网站上看到的那样(也是通过boost侵入的boost实现),
pointer_traits
并不专门用于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. 虽然这不符合强烈的动机反对指定应该存在针对
T* const
的pointer_traits<>
的特化,但我想解释为什么它不包括在内的原因可能是pointer_traits<>
主要用于在上下文中使用发生模板参数推导(特别是类型推导)。
Since type deduction disregards top-level cv-qualifications, a specialization for T* const
or T* volatile
or T* const volatile
was probably deemed unnecessary: 由于类型推导忽略了顶级cv资格,因此可能认为
T* const
或T* volatile
或T* const volatile
是不必要的:
#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. 当然,这并不意味着在这种情况下对
T* cv
进行专业化会造成伤害,我只是想提供一个可能解释为什么缺少这些专业化的原因。
Similarly, no specialization of iterator_traits<>
is provided for T* cv
. 同样,没有为
T* cv
提供iterator_traits<>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.