nullptr 右值如何

How is nullptr rvalue

在查看 nullptr here 的实现时,引起我注意的是 nullptrrvalue 这意味着我们可以做这样的事情

std::nullptr_t&& nullref = nullptr;

但是 nullptr 怎么会是 rvalue 因为实现是这样的

const class {...} nullptr = {};

这是核心功能吗?我错过了什么?

实施与它无关。

定义关键字 nullptr 以生成右值表达式,到此结束。

[C++14: 2.14.7/1]: The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [ Note: std::nullptr_t is a distinct type that is neither a pointer type nor a pointer to member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value. See 4.10 and 4.11. —end note ]

我同意您不能自己在用户空间中根据此标准重新实现它,但其他每个关键字也是如此。

仅由关键字 truefalse 组成的表达式也是右值,如果您好奇的话。

[C++14: 2.14.6/1]: The Boolean literals are the keywords false and true. Such literals are prvalues and have type bool.

如果备选方案 #1 是 nullptr 的定义,那么您认为它是左值是对的。但是,可以使用类似这样的方法将其强制为右值:

const class __nullptr_t {...} __nullptr = {};
#define nullptr (__nullptr_t(__nullptr));

虽然这不是最终标准化的内容。在实际的 C++11 中,nullptr 是一个文字,与 3.14'x'.

相同