本文介绍了nullptr不是特殊的关键字和std :: nullptr_t的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为这是一个关键字。我目前的gcc不以不同的阴影突出显示 nullptr 。为了验证这一点,我写了以下:

  void *& p = nullptr; 

所以我从错误中得到了一些线索:

如果 nullptr 是一个对象,那么它实际上是一个等价于简单 0 ?换句话说,假设我写:

  #define NULL nullptr 

上面的语句不会改变我的代码中的任何东西吗?此外,有趣的是知道 std :: nullptr_t 类型的其他用例。

解决方案

这是一个关键字,标准草案说明( lex.nullptr ):

nullptr 不是一个指针,而是可以转换为指针类型。这禁止你的上述赋值,这是一个无关的引用类型的赋值,在这种情况下不可能进行转换(考虑 int& a = 1.f; ! / p>

Doing #define NULL nullptr 不应该改变行为,除非你使用 NULL 在上下文中,例如 int i = 4; if(NULL == i){} ,因为 nullptr

我不认为有很多其他用例 std :: nullptr_t ,它只是一个哨兵,因为 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:

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.

解决方案

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 (consider int& a = 1.f;!).

Doing #define NULL nullptr shouldn't alter the behaviour unless you did use NULL in a context such as int i = 4; if(NULL == i) {}, which won't work with nullptr because nullptr 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 because nullptr needs a type.

这篇关于nullptr不是特殊的关键字和std :: nullptr_t的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 06:37