简体   繁体   English

为什么没有为“T * const”定义pointer_traits?

[英]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* constpointer_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* constT* volatileT* 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.

相关问题 pointer_traits为既不是X <A,T ...>也提供成员typedef element_type的类型提供什么? - What does pointer_traits provide for types that are neither X<A, T…> nor provide a member typedef element_type? std :: allocator_traits ::带有const指针的构造 - std::allocator_traits::construct with const pointer 为什么将“指向非常量的指针的指针”转换为“指向常量的指针的指针”是不合法的 - Why isn't it legal to convert "pointer to pointer to non-const" to a "pointer to pointer to const" 为什么不能映射:::指向const的指针? - Why can't map::find a pointer to a const? 为什么const限定符不能处理const对象上的指针成员? - Why isn't the const qualifier working on pointer members on const objects? 为什么不能在之后设置 const 指针? - Why can't a const pointer be set afterwards? 为什么不能用另一个指向const char的指针初始化一个指针? - Why can't a pointer be initialized with another pointer to a const char? 为什么在“ const typename iterator_traits”中忽略“ const” <RandomIterator> ::参考”? - Why “const” is ignored in “const typename iterator_traits<RandomIterator>::reference”? 删除指向 const (T const*) 的指针 - Deleting a pointer to const (T const*) 为什么它是const指针而不是const指针? - Why it is a const pointer rather than pointer to const?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM