简体   繁体   English

为什么是“std::is_pointer<std::nullptr_t> ::value" 等于 false?</std::nullptr_t>

[英]Why is "std::is_pointer<std::nullptr_t>::value" equal to false?

I read about std::is_pointer in C++.我在 C++ 中读到了std::is_pointer is_pointer。

Then I wrote the program and check whether T is a pointer type or not using std::is_pointer .然后我编写程序并使用std::is_pointer检查T是否为指针类型。

#include <iostream>

int main() 
{
    std::cout<<std::boolalpha;
    std::cout<<"char : " <<std::is_pointer<char>::value<<std::endl; // OK
    std::cout<<"char * : "<<std::is_pointer<char *>::value<<std::endl; // OK
    std::cout<<"char ** : "<<std::is_pointer<char **>::value<<std::endl; // OK
    std::cout<<"char *** : "<<std::is_pointer<char ***>::value<<std::endl; // OK
    std::cout<<"std::nullptr_t : "<<std::is_pointer<std::nullptr_t>::value<<std::endl;  // Not ok, Why false??
}

Output : [Wandbox Demo] Output[Wandbox 演示]

char : false
char * : true
char ** : true
char *** : true
std::nullptr_t : false

Why is std::is_pointer<std::nullptr_t>::value equal to false ?为什么std::is_pointer<std::nullptr_t>::value等于false

Because std::nullptr_t is not a pointer type . 因为std::nullptr_t 不是指针类型 And is_pointer only evaluates to true for pointer types. 并且is_pointer仅对指针类型计算为true

nullptr_t is convertible to a pointer, but it isn't a pointer. nullptr_t可以转换为指针,但它不是指针。 Indeed, nullptr_t is not a class type, integral, floating-point, enumerator, or any kind of type other than is_null_pointer type. 实际上, nullptr_t不是类类型,整数,浮点数,枚举器或is_null_pointer类型以外的is_null_pointer类型。 It has its own unique classification in the categorization of types. 在类型分类中,它具有自己独特的分类。

Because it's not a pointer type. 因为它不是指针类型。 It's a distinct type that is implicitly convertible to any pointer type. 这是一个独特的类型,可以隐式转换为任何指针类型。

As the note in [lex.nullptr] summarizes: [lex.nullptr]中的注释总结:

The pointer literal is the keyword nullptr. 指针文字是关键字nullptr。 It is a prvalue of type std​::​nullptr_t. 它是std :: nullptr_t类型的prvalue。 [ Note: std​::​nullptr_t is a distinct type that is neither a pointer type nor a pointer to member type; [注意:std :: nullptr_t是一个独特的类型,既不是指针类型,也不是成员类型的指针; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value. 相反,此类型的prvalue是空指针常量,并且可以转换为空指针值或空成员指针值。 See [conv.ptr] and [conv.mem]. 请参阅[conv.ptr]和[conv.mem]。 — end note ] —尾注]

As unintuitive as it sounds, std::nullptr_t is not a pointer type. 听起来不直观, std::nullptr_t不是指针类型。 For instance, you cannot dereference a std::nullptr_t , so it'd be pretty weird if is_pointer_type<nullptr_t>::value was true . 例如, std::nullptr_t引用std::nullptr_t ,因此如果is_pointer_type<nullptr_t>::valuetrue ,这将很奇怪。

It is merely convertible to any pointer type. 它仅可转换为任何指针类型。

Because std::nullptr_t is not a pointer type itself, but the type of the null pointer literal nullptr . 因为std :: nullptr_t本身不是指针类型,而是null指针文字nullptr的类型。

std::nullptr_t is the type of the null pointer literal, nullptr . std::nullptr_t是空指针文字nullptr的类型。 It is a distinct type that is not itself a pointer type or a pointer to member type. 它是一个独特的类型,它本身不是指针类型或成员类型的指针。

From the standard, 21.2.3 Null pointers [support.types.nullptr] 从标准21.2.3空指针开始[support.types.nullptr]

The type nullptr_t is a synonym for the type of a nullptr expression, and it has the characteristics described in [basic.fundamental] and [conv.ptr]. 类型nullptr_tnullptr表达式类型的同义词,并且具有[basic.fundamental]和[conv.ptr]中描述的特征。

It's definitely unintuitive in some cases, but it makes sense:在某些情况下这绝对是不直观的,但它是有道理的:

  1. Value-wise, it doesn't behave like a pointer;在价值方面,它的行为不像指针; it cannot point to anything .它不能指向任何东西
    For example, you cannot do例如,你不能做

    T *p =...; reinterpret_cast<T *>(reinterpret_cast<std::nullptr_t>(p));

    and expect to get a pointer to the same address that p points to.并期望获得指向p指向的相同地址的指针。

  2. Type-wise, it doesn't behave like a pointer.在类型方面,它的行为不像指针。 For example,例如,

     std::is_pointer<P>::value

    should imply应该暗示

    std::is_same<P, std::remove_pointer<P>::type *>::value

    but that clearly isn't the case.但事实显然并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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