[英]What is the type of nullptr?
The Standard states, that nullptr
is a pointer literal of type std::nullptr_t
(2.14.7). 标准规定,
nullptr
是std::nullptr_t
(2.14.7)类型的指针文字。 And 18.2p9 defines nullptr_t
by 并且18.2p9定义了
nullptr_t
namespace std {
typedef decltype(nullptr) 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). 通过7.1.6.2p4,
decltype(nullptr)
是表达式nullptr
的类型,根据定义std::nullptr_t
(因为表达式nullptr
是一个prvalue)。 Substituting that into the definition of nullptr_t
results in 将其替换为
nullptr_t
的定义nullptr_t
导致
typedef nullptr_t nullptr_t
On the other hand a typedef specifier does not introduce a new type, it's just a name for another existing type. 另一方面,typedef说明符不引入新类型,它只是另一个现有类型的名称。 So, what is exactly
nullptr_t
? 那么,究竟是什么
nullptr_t
? I'm not able to comprehend these definitions. 我无法理解这些定义。
It is implementation-specific. 它是特定于实现的。 What is important is that (p. 18.2/9 of the C++11 Standard):
重要的是(C ++ 11标准的第18.2 / 9页):
[...] The type for which
nullptr_t
is a synonym has the characteristics described in 3.9.1 and 4.10.[...]
nullptr_t
是同义词的类型具有3.9.1和4.10中描述的特征。 [...][...]
As long as it behaves like the Standard specifies in those two paragraphs, it can be anything. 只要它的行为与标准在这两段中指定的一样,它就可以是任何东西。
I believe the logical fallacy in your argument is that this: 我相信你的论证中的逻辑谬误是:
By 7.1.6.2p4
decltype(nullptr)
is the type of the expressionnullptr
, which is by definitionstd::nullptr_t
(since the expressionnullptr
is a prvalue)通过7.1.6.2p4,
decltype(nullptr)
是表达式nullptr
的类型,根据定义std::nullptr_t
(因为表达式nullptr
是一个prvalue)
Does not mean that nullptr_t
is not a type alias. 并不意味着
nullptr_t
不是类型别名。 For instance, if I define: 例如,如果我定义:
typedef decltype(42) foo;
I can say that the type of the expression: 我可以说表达式的类型:
42
Is foo
. 是
foo
。 Yet, foo
is just an alias for another type ( int
). 然而,
foo
只是另一种类型( int
)的别名。
Internally there is an entity that is the null pointer constant type . 在内部有一个实体,它是空指针常量类型 。 It is one of the fundamental types.
它是基本类型之一。
The keyword, literal and expression nullptr
has this type. 关键字,literal和expression
nullptr
具有此类型。 decltype(nullptr)
refers to this type. decltype(nullptr)
指的是这种类型。
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. 但是, 名称
std::nullptr_t
不是关键字(甚至不是上下文敏感的关键字),因此在声明之前名称不存在。 If you refer to the name std::nullptr_t
without declaring it, it is an error, as for any undeclared name. 如果在没有声明的情况下引用名称
std::nullptr_t
,那么对于任何未声明的名称都是错误的。
So although the type exists at the start of translation like any fundamental type, the name does not exist. 因此,虽然类型存在于任何基本类型的转换开始处,但该名称不存在。
In fact there are other fundamental types that do not have a "single spelling", such as short int. 事实上,还有其他基本类型没有“单一拼写”,例如short int。 A short int can be refered to as
short
, short int
, signed short int
, signed short
, or any permutation thereof. short int可以被称为
short
, short int
, signed short int
, signed short
或其任何排列。
It is also not dissimilar to the relationship between the typeid
operator (keyword), and the type of the typeid(...)
expression, std::typeinfo
. 它与
typeid
运算符(关键字)和typeid(...)
表达式的类型std::typeinfo
之间的关系也没有什么不同。 typeinfo
is also not a keyword and the name does not exist before being declared. typeinfo
也不是关键字,并且在声明之前名称不存在。
Basically, you are conflating an entity (the null pointer constant type) with a name ( std::nullptr_t
) 基本上,您将实体 (空指针常量类型)与名称 (
std::nullptr_t
) std::nullptr_t
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. 如果你问为什么语言设计者没有指定
nullptr_t
和typeinfo
作为关键字,我会推测它们不常见,冒着与具有相同拼写的用户定义名称发生名称冲突的风险。 Recall that such a collision would occur in any and all scopes. 回想一下,任何和所有范围都会发生这种冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.