简体   繁体   English

为什么nullptr_t不是关键字

[英]Why is nullptr_t not a keyword

Here is a declaration of nullptr_t in <cstddef> : 以下是<cstddef>nullptr_t的声明:

namespace std {
  typedef decltype(nullptr) nullptr_t;
}

According to this , std::nullptr_t is an alias for some unspecified fundamental type of which nullptr is an instance. 根据这个std::nullptr_t是某些未指定的基本类型的别名,其中nullptr是一个实例。 So the actual type of nullptr doesn't have a name (well, the language does not give it a name, the name was given by standard library). 所以nullptr的实际类型没有名称(嗯,语言没有给它起一个名字,名称是由标准库给出的)。

nullptr itself is a keyword. nullptr本身是一个关键字。 But standard did not introduce a keyword for type of nullptr . 但是标准没有为nullptr类型引入关键字。 Instead using decltype(nullptr) is offered. 而是使用decltype(nullptr)

What are reasons for doing this? 这样做的原因是什么? I found it much confusing. 我觉得很困惑。 You need to include header and specify std:: for just using a language built-in feature. 您需要包含标头并仅使用语言内置功能指定std::

Is this to keep the set of C++ keywords as small as possible? 这是为了保持C ++关键字的集合尽可能小吗? Is this specifically for nullptr or committee is going to declare all new types like this, so we would have namespace std { typedef decltype(false) bool; } 这是专门针对nullptr或委员会将要声明所有这样的新类型,所以我们将namespace std { typedef decltype(false) bool; } namespace std { typedef decltype(false) bool; } if such decision was made earlier? namespace std { typedef decltype(false) bool; }如果这样的决定早些时候?

According to the initial proposal of nullptr , N2431 ( Emphasis Mine ): 根据nullptr的初步建议, N2431重点矿 ):

We propose a new standard reserved word nullptr . 我们提出了一个新的标准保留字nullptr The nullptr keyword designates a constant rvalue of type decltype(nullptr) . nullptr关键字指定类型为decltype(nullptr)的常量rvalue。 We also provide the typedef : typedef decltype(nullptr) nullptr_t; 我们还提供了typedeftypedef decltype(nullptr) nullptr_t; nullptr_t is not a reserved word. nullptr_t不是保留字。 It is a typedef (as its _t typedef indicates) for decltype(nullptr) defined in <cstddef> . 它是<cstddef>定义的decltype(nullptr)typedef (如_t typedef所示)。 We do not expect to see much direct use of nullptr_t in real programs. 我们不希望看到在实际程序中直接使用nullptr_t。

Generally, the committee is reluctant in the addition of new keywords in the language. 一般来说,委员会不愿意在语言中添加新的关键词。 In my humble opinion this is for a good reason, named backward compatibility. 我谦虚地认为这是一个很好的理由,称为向后兼容性。

If you further read the proposal, you'd realize that the main concern is not breaking existing code. 如果您进一步阅读该提案,您会发现主要问题不在于破坏现有代码。

Imagine the effect that would have if the committee now and then introduced a new keyword. 想象一下,如果委员会现在然后引入一个新的关键字,会产生什么影响。 All hell would break loose and C++ from a success story would have been a big joke. 所有的地狱都会破裂,C ++从成功的故事开始就是一个大笑话。

I believe the reason is simple: the standardization committee wants to avoid as much as reasonably possible to introduce new keywords (because, given the billions lines of existing C++ code, it is likely to conflict with some code somewhere). 我认为原因很简单:标准化委员会希望尽可能避免引入新的关键字(因为,考虑到数十亿行现有 C ++代码,它可能会与某些代码冲突)。

Since std::nullptr_t is definable, it does not need to be a keyword. 由于std::nullptr_t是可定义的,因此它不需要是关键字。

And bool is a keyword for historical reasons. 由于历史原因, bool是一个关键字。 It very probably has been introduced quite early... 很可能很早就推出了......

C++ is mostly about legacy software, so human and social and economical considerations (and backward compatibility) matters a lot for the standardization committee (often more than technical reasons alone). C ++主要是关于遗留软件,因此人类和社会和经济方面的考虑(以及向后兼容性)对标准化委员会来说很重要(通常仅仅是技术原因)。

Adding new keywords are avoided when not necessary. 在不需要时,可以避免添加新关键字。 Why add something to the language when it can just be added to the library. 为什么在可以添加到库中时添加某些语言。

You already have a similar thing with sizeof(x) which returns std::size_t , which you also have to include a header to get the typedef for. 你已经有了类似的东西, sizeof(x)返回std::size_t ,你还必须包含一个头来获取typedef。

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

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