为什么nullptr_t不是关键字 [英] Why is nullptr_t not a keyword

查看:286
本文介绍了为什么nullptr_t不是关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 nullptr_t < cstddef> 中的声明:

  namespace std {
typedef decltype(nullptr)nullptr_t;
}

根据 std :: nullptr_t 是一些未指定的基本类型的别名, code> nullptr 是一个实例。因此, nullptr 的实际类型没有名称(嗯,语言没有给它一个名字,这个名字是由标准库给出的)。



nullptr 本身是一个关键字。但标准没有引入 nullptr 类型的关键字。而是使用 decltype(nullptr)



这是什么原因?我发现很混乱。您需要包含标题,并指定 std :: 才能使用语言内置功能。<​​/ p>

保持C ++关键字的集合尽可能小?这是专门为 nullptr 或委员会要声明所有新类型像这样,所以我们将有 namespace std {typedef decltype(false)bool; 如果这样的决定是早些时候?

根据 nullptr N2431 Emphasis Mine ):


我们建议一个新的标准保留字 nullptr nullptr 关键字
指定类型 decltype(nullptr)的常量右值。我们还
提供 typedef typedef decltype(nullptr)nullptr_t; nullptr_t
不是保留字
typedef _t
typedef 表示)for
decltype(nullptr) c $ c>< cstddef>
我们不希望在实际程序中看到很多
直接使用nullptr_t。


,委员会不愿在该语言中添加新的关键字。



如果你进一步阅读了这个建议,你会意识到,主要关注的是不打破现有的代码。



想象一下,如果委员会现在,然后介绍一个新的关键字的效果。所有的地狱都会崩溃,C ++从一个成功的故事将是一个大笑话。


Here is a declaration of nullptr_t in <cstddef> :

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. 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 itself is a keyword. But standard did not introduce a keyword for type of nullptr. Instead using decltype(nullptr) is offered.

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.

Is this to keep the set of C++ keywords as small as possible? 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; } if such decision was made earlier?

解决方案

According to the initial proposal of nullptr, N2431 (Emphasis Mine):

We propose a new standard reserved word nullptr. The nullptr keyword designates a constant rvalue of type decltype(nullptr). We also provide the typedef: typedef decltype(nullptr) nullptr_t; nullptr_t is not a reserved word. It is a typedef (as its _t typedef indicates) for decltype(nullptr) defined in <cstddef>. We do not expect to see much direct use of nullptr_t in real programs.

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.

这篇关于为什么nullptr_t不是关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆