简体   繁体   English

为什么 nullptr 不需要 header 而 nullptr_t 需要

[英]why does nullptr not require header while nullptr_t does

nullptr_t is the type for nullptr . nullptr_tnullptr的类型。 When I use nullptr_t , I have to use the header file <cstddef> but why isn't it required when using the nullptr keyword alone?当我使用nullptr_t时,我必须使用 header 文件<cstddef>但为什么单独使用nullptr关键字时不需要它?

As per [lex.key]/1 , nullptr is a keyword:根据[lex.key]/1nullptr是一个关键字:

The identifiers shown in Table 5 are reserved for use as keywords (that is, they are unconditionally treated as keywords in phase 7) [...]表 5 中显示的标识符保留用作关键字(即,它们在阶段 7 中被无条件地视为关键字)[...]

[tab:lex.key] : ... nullptr [tab:lex.key] : ... nullptr

Whereas std::nullptr_t is an alias declaration defined by the standard.std::nullptr_t是标准定义的别名声明。 Particularly, as of [cstddef.syn] , is is defined as the type of the a nullptr expression:特别是,从[cstddef.syn] 开始,is 被定义为nullptr表达式的类型:

Header <cstddef> synopsis Header <cstddef>概要

... ...

 using nullptr_t = decltype(nullptr);

as specified in [support.types.nullptr]/1 :[support.types.nullptr]/1中指定:

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_t类型是nullptr表达式类型的同义词,它具有 [basic.fundamental] 和[conv.ptr]中描述的特征。

Thus, to make use of std::nullptr_t , you need to include the <cstddef> header, but you can likewise use decltype(nullptr) if you simply want the same underlying type but without using the std::nullptr_t alias.因此,要使用std::nullptr_t ,您需要包含<cstddef> header,但如果您只是想要相同的基础类型而不使用std::nullptr_t别名,则同样可以使用decltype(nullptr)

nullptr is a keyword, std::nullptr_t is a type defined by the C++ Stadnard library. nullptr是关键字, std::nullptr_t是 C++ Stadnard 库定义的类型。 Keywords are not introduced by header files, they are "wired" in the compiler itself.关键字不是由 header 文件引入的,它们是在编译器本身中“连接”的。

Most types defined by C++ are defined via the standard library. C++ 定义的大多数类型都是通过标准库定义的。 Only a handful — char , int … — are defined via keywords, and they are all old (ie they have been part of C++ since the beginning).只有少数—— charint ……——是通过关键字定义的,而且它们都是旧的(即它们从一开始就是 C++ 的一部分)。 Other types ( std::byte , std::initializer_list , …) are not.其他类型( std::bytestd::initializer_list ……)不是。 There are many reasons for this, but mainly it just isn't necessary to define new keywords, and defining new keywords is intrusive and breaks existing code that uses the same identifier, so it's avoided when possible.这有很多原因,但主要是没有必要定义新关键字,而且定义新关键字是侵入性的,会破坏使用相同标识符的现有代码,因此尽可能避免。

The real question therefore is: why did C++ define a keyword for nullptr ?因此,真正的问题是:为什么 C++ 为nullptr定义了关键字? The original proposal explicitly lists this as a design goal. 原始提案明确将此列为设计目标。 It gives the following reasons, amongst others:它给出了以下原因,其中包括:

  1. It doesn't require including a header to use the value.它不需要包含 header 即可使用该值。
  2. It makes it easier for compiler vendors to produce good error messages involving null pointers.它使编译器供应商更容易生成涉及 null 个指针的良好错误消息。
  3. Implementing the nullptr value via C++ code in a header is challenging to make it work correctly in constant expressions.通过 header 中的 C++ 代码实现nullptr值很难使其在常量表达式中正常工作。 I believe that this point is no longer relevant since probably C++17, but it was definitely a concern in C++11, when the proposal was introduced.我认为这一点可能从 C++17 开始就不再相关了,但在 C++11 提出该提案时,它肯定是一个问题。

Reason (1) on its own might not seem compelling, since the same reason would apply to the type name nullptr_t .原因 (1) 本身似乎并不令人信服,因为同样的原因也适用于类型名称nullptr_t But in actual usage, nullptr is a lot more common;但在实际使用中, nullptr更为常见; code rarely needs to name the null pointer type .代码很少需要命名 null 指针类型

Taken together, these are strong reasons to make the null pointer value constant a built-in keyword.综上所述,这些是使 null 指针值常量成为内置关键字的有力理由。 But, as noted, the reasons for making the type name also keyword aren't nearly as strong.但是,如前所述,将类型名称也作为关键字的原因并不那么充分。

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

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