nullptr 不是一个特殊的关键字和 std::nullptr_t 的对象吗?
可能的重复:
nullptr 到底是什么?
我首先认为它是一个关键字。我现在的 gcc 不会以不同的阴影突出显示 nullptr
。为了验证这一点,我写了以下内容:
void *&p = nullptr;
所以我从错误中得到了一些线索:
错误:“void*&”类型的非常量引用的初始化无效 来自“std::nullptr_t”类型的右值
如果 nullptr
是一个对象,那么它真的是一个相当于简单 0
的指针吗?换句话说,假设我写:
#define NULL nullptr
上述语句不会改变我的代码中的任何内容吗?此外,了解 std::nullptr_t 类型的其他用例也会很有趣。
Possible Duplicate:
What exactly is nullptr?
I first thought it's a keyword. My present gcc doesn't highlight nullptr
in a different shade. To verify that, I wrote following:
void *&p = nullptr;
So I got some clue from the error that:
error: invalid initialization of non-const reference of type ‘void*&’
from an rvalue of type ‘std::nullptr_t’
If nullptr
is an object then is it really a pointer equivalent of simple 0
? In other word, suppose I write:
#define NULL nullptr
Is the above statement doesn't alter anything in my code ? Also, it would be interesting to know other use cases for std::nullptr_t
type as such.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它是一个关键字,标准草案说 (lex.nullptr):
nullptr
还不是指针,但它可以转换为指针类型。这会禁止您的上述分配,这是对不相关引用类型的分配,在这种情况下不可能进行转换(考虑int& a = 1.f;
!)。执行
#define NULL nullptr
不应改变行为,除非您确实在诸如int i = 4; 之类的上下文中使用了
,它不适用于NULL
。 if(NULL == i) {}nullptr
,因为nullptr
不能被视为整数文字。我认为
std::nullptr_t
没有很多其他用例,它只是一个哨兵,因为nullptr
需要一个类型。It is a keyword, the standard draft says (lex.nullptr):
the
nullptr
is not yet a pointer, but it can be converted to a pointer type. This forbids your above assignment, which is an assignment to an unrelated reference type, in which case no conversion is possible (considerint& a = 1.f;
!).Doing
#define NULL nullptr
shouldn't alter the behaviour unless you did useNULL
in a context such asint i = 4; if(NULL == i) {}
, which won't work withnullptr
becausenullptr
is can't be treated as an integer literal.I don't think there are many other use-cases for
std::nullptr_t
, it's just a sentinel becausenullptr
needs a type.nullptr
是表示空指针常量的关键字。它的类型为 nullptr_t,可隐式转换并可与任何指针类型或指向成员的指针类型进行比较。阅读这些,
nullptr
is a keyword that represents null pointer constant. It is of typenullptr_t
, which is implicitly convertible and comparable to any pointer type or pointer-to-member type.Read these,
nullptr
不是一个对象,就像0
不是一个整数对象一样。前者是std::nullptr_t
类型的纯右值(即一种表达式),后者是int< 类型的整数文字(也是一种表达式,也是一种纯右值) /代码>。
可以使用这样的表达式初始化对象:
不可能使用这样的表达式初始化左值引用,因为它们是纯右值;左值引用只能从左值初始化。
nullptr
is not an object just like0
is not an integer object. The former is a prvalue (i.e. a kind of expression) of typestd::nullptr_t
and the latter is an integer literal (also a kind of expression and also a prvalue) of typeint
.It is possible to initialize an object with such expressions:
It is not possible to initialize an lvalue reference with such expressions because they are prvalues; an lvalue reference can only be initialized from an lvalue.
nullptr
确实是一个关键字,标准要求类型std::nullptr_t
等同于typedef decltype(nullptr) nullptr_t;
以启用重载基于nullptr
。nullptr
is indeed a keyword and the standard demands a typestd::nullptr_t
to be equivalent totypedef decltype(nullptr) nullptr_t;
to enable overloading based onnullptr
.nullptr
将成为下一个 C++ 标准(现在称为 C++0x)中的关键字。需要消除
f(int)
和f(T*)
之间的歧义,因此它不是简单的 0,而是nullptr_t
。我不知道 gcc 可以突出显示代码;-)
nullptr
will be a keyword in next C++ standard, now called C++0x.It is needed to disambiguate between
f(int)
andf(T*)
, so it's not simply 0, but ofnullptr_t
.I didn't know gcc can highlight code ;-)