11

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;
}

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

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.

4

2 回答 2

10

它是特定于实现的。重要的是(C++11 标准的第 18.2/9 页):

[...] 作为nullptr_t同义词的类型具有 3.9.1 和 4.10 中描述的特征。[...]

只要它的行为与标准在这两段中指定的一样,它可以是任何东西。

我相信你的论点中的逻辑谬误是:

7.1.6.2p4decltype(nullptr)是表达式的类型nullptr,这是根据定义std::nullptr_t(因为表达式nullptr是纯右值)

不代表不是nullptr_t类型别名。_ 例如,如果我定义:

typedef decltype(42) foo;

我可以说表达式的类型:

42

foo。然而,foo它只是另一种类型 ( int) 的别名。

于 2013-06-12T15:30:13.577 回答
4

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

关键字、文字和表达式nullptr具有这种类型。 decltype(nullptr)指这种类型。

但是,名称 std::nullptr_t不是关键字(甚至不是上下文相关的关键字),因此名称在声明之前不存在。如果您在没有声明的情况下引用该名称std::nullptr_t,则与任何未声明的名称一样,这是一个错误。

因此,尽管类型在翻译开始时就存在,就像任何基本类型一样,但名称并不存在。

事实上,还有其他基本类型没有“单一拼写”,例如 short int。短整数可以称为short, short int, signed short int, signed short, 或其任何排列。

typeid它与运算符(关键字)和typeid(...)表达式类型之间的关系也没有什么不同std::typeinfotypeinfo也不是关键字,并且名称在声明之前不存在。

基本上,您将实体(空指针常量类型)与名称( std::nullptr_t)

如果您问为什么语言设计者没有将nullptr_tand指定typeinfo为关键字,我会推测它们不够常见,不会冒与具有相同拼写的用户定义名称发生名称冲突的风险。回想一下,这种冲突会发生在任何和所有范围内。

于 2013-06-12T16:06:11.040 回答