什么是nullptr的类型? [英] What is the type of nullptr?

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

问题描述

标准规定 nullptr 是类型 std :: nullptr_t (2.14.7)的指针文字。和18.2p9定义 nullptr_t

The Standard states, that nullptr is a pointer literal of type std::nullptr_t (2.14.7). And 18.2p9 defines nullptr_t by

namespace std {
  typedef decltype(nullptr) nullptr_t;
}

通过7.1.6.2p4 decltype(nullptr)是表达式 nullptr 的类型,按照定义 std :: nullptr_t 表达式 nullptr 是一个prvalue)。将 nullptr_t 的定义替换成

By 7.1.6.2p4 decltype(nullptr) is the type of the expression nullptr, which is by definition std::nullptr_t (since the expression nullptr is a prvalue). Substituting that into the definition of nullptr_t results in

typedef nullptr_t nullptr_t

另一方面,typedef说明符不引入新类型,它只是另一个现有类型的名称。那么,什么是 nullptr_t ?我不能理解这些定义。

On the other hand a typedef specifier does not introduce a new type, it's just a name for another existing type. So, what is exactly nullptr_t? I'm not able to comprehend these definitions.

推荐答案

内部有一个实体是 null指针常量类型。它是基本类型之一。

Internally there is an entity that is the null pointer constant type. It is one of the fundamental types.

关键字,文字和表达式 nullptr 有此类型。 decltype(nullptr)是指此类型。

The keyword, literal and expression nullptr has this type. decltype(nullptr) refers to this type.

但是 $ c> std :: nullptr_t 不是关键字(甚至不是上下文敏感的),因此该名称直到声明才存在。如果你引用名称 std :: nullptr_t 而不声明它,它是一个错误,对于任何未声明的名称。

However the name std::nullptr_t is not a keyword (not even a context-sensitive one), and so the name does not exist until declared. If you refer to the name std::nullptr_t without declaring it, it is an error, as for any undeclared name.

因此,虽然类型存在于翻译开头,像任何基本类型一样,名称不存在。

So although the type exists at the start of translation like any fundamental type, the name does not exist.

事实上,还有其他基本类型没有单个拼写,例如short int。 short int可以被称为 short short int signed short int 签署短或其任何排列。

In fact there are other fundamental types that do not have a "single spelling", such as short int. A short int can be refered to as short, short int, signed short int, signed short, or any permutation thereof.

typeid 运算符(关键字)和 typeid(...)表达式的类型之间, std :: typeinfo typeinfo 也不是一个关键字,并且该名称在声明之前不存在。

It is also not dissimilar to the relationship between the typeid operator (keyword), and the type of the typeid(...) expression, std::typeinfo. typeinfo is also not a keyword and the name does not exist before being declared.

基本上,你是conflating具有名称 std :: nullptr_t

Basically, you are conflating an entity (the null pointer constant type) with a name (std::nullptr_t)

如果你问为什么语言设计师不指定 nullptr_t typeinfo 关键字,我推测他们不是常见的风险与用户定义的名称冲突与相同的拼写。回想一下,这种碰撞将发生在任何和所有范围内。

If you ask why didn't the language designers specify nullptr_t and typeinfo as keywords, I would speculate that they are not common enough to risk a name collision with a user-defined name with the same spelling. Recall that such a collision would occur in any and all scopes.

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

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