简体   繁体   English

nullptr有哪些不同的类型?

[英]What are the different nullptr types?

I was reading this question : 我在读这个问题:

In c++11, does dynamic_cast return nullptr or 0? 在C ++ 11中,dynamic_cast是否返回nullptr或0?

I learned that the result of a dynamic_cast can be the null value for that particular type: 我了解到dynamic_cast的结果可以是该特定类型的null值:

Consider a variable char * ptr. 考虑一个变量char * ptr。 It's type is (unsurprisingly) char *. 它的类型是(毫无疑问)char *。 But the type of nullptr is the special type nullptr_t. 但是nullptr的类型是特殊类型的nullptr_t。 So when we write something like ptr = nullptr, some technical things must happen 因此,当我们编写类似ptr = nullptr的内容时,必须发生一些技术问题

nullptr must be implicitly converted to char *. nullptr必须隐式转换为char *。 The result of this conversion is set as the new value of ptr. 转换的结果设置为ptr的新值。 The null value for char * is the result of converting nullptr to char *. char *的空值是将nullptr转换为char *的结果。 Conceptually, it's still nullptr, but with a different type (char *). 从概念上讲,它仍然是nullptr,但是具有不同的类型(char *)。 This null value is distinct from the null value of int * or string * or any other pointer type. 此null值不同于int *或string *或任何其他指针类型的null值。 We tend to think of these null values as just nullptr (or 0), but each one is really a distinct value from a different type. 我们倾向于将这些null值视为nullptr(或0),但每个值实际上都是与不同类型不同的值。 (By the way, the same conversion happens for comparison using ==). (顺便说一下,使用==进行比较时会发生相同的转换)。

So what are the different null pointer types ? 那么什么是空指针类型呢? Does it really matter ? 真的有关系吗 ? Is it sufficient in any check (where we want to determine if something is null) to simply check if == to std::nullptr ? 在任何检查(我们要确定某项是否为空)中仅检查==到std :: nullptr是否足够?

There aren't different nullptr types. 没有不同的nullptr类型。 nullptr is a value of type std::nullptr_t . nullptrstd::nullptr_t类型的值。 There are different pointer types, and any pointer type can have a null value, which when compared to nullptr , results in true . 有不同的指针类型,并且任何指针类型都可以具有null值,与nullptr相比,其结果为true And the number of pointer types is infinite. 指针类型的数量是无限的。 Since you can form a pointer to any type, and the result is itself a type which you can form a pointer to. 由于您可以形成指向任何类型的指针,因此结果本身就是可以形成指向的类型的指针。 (ie you can have int************************* ) (即,您可以拥有int*************************

Is it sufficient in any check (where we want to determine if something is null) to simply check if == to std::nullptr ? 在任何检查(我们要确定某项是否为空)中仅检查==到std :: nullptr是否足够?

Not std::nullptr , just nullptr . 不是std::nullptr ,只是nullptr But otherwise, yes. 但是否则,是的。

So what are the different null pointer types? 那么什么是空指针类型呢?

There is only one null pointer type, nullptr_t . 只有一种空指针类型, nullptr_t

Does it really matter? 真的有关系吗?

Since there is only one type of null pointer, this question is moot. 因为只有一种类型的空指针,所以这个问题没有意义。

Is it sufficient in any check (where we want to determine if something is null) to simply check if == to std::nullptr ? 在任何检查(我们要确定某项是否为空)中仅检查==到std::nullptr是否足够?

There is nothing like std::nullptr . 没有什么像std::nullptr nullptr is a keyword. nullptr是一个关键字。 It is a prvalue of type std::nullptr_t . 它是std::nullptr_t类型的prvalue

Yes, any pointer can be compared with nullptr . 是的,任何指针都可以与nullptr进行比较。

There is a type std::nullptr_t . 有一个类型std::nullptr_t It is not a pointer type. 它不是指针类型。 Despite this fact, we call an instance of this type (such as nullptr ) a "null pointer constant". 尽管如此,我们nullptr这种类型的实例(例如nullptr )称为“空指针常量”。

For every type T , T* is a distinct pointer type, and there is a null pointer of this type. 对于每种类型TT*是一个不同的指针类型,并且存在这种类型的空指针。

There are suitable conversions from nullptr_t to any pointer type that allow us to use nullptr whenever we want a null pointer constant. nullptr_t到任何指针类型都有适当的转换,允许我们在需要空指针常量时使用nullptr eg 例如

int *ptr = nullptr; // initialize to be the null pointer of type int*

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

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