空指针常量转换为prvalue

时间:2014-07-31 14:02:58

标签: c++ null

由以下规则sec描述的空指针常量转换。 4.10 / 1 N3797:

  

整数类型的空指针常量可以转换为prvalue   类型为std :: nullptr_t。 [注意:生成的prvalue不是null   指针值。 - 后注]

我认为以下是这种转换的一个例子:

static_cast<std::nullptr_t>(0);

但似乎不是这样,因为

#include <iostream>
#include <iostream>
using namespace std;

int main()
{
    void *p = static_cast<std::nullptr_t>(0);
    cout << p; //0- Why? It is not a null pointer value
}

demo

1 个答案:

答案 0 :(得分:2)

表达式p 空指针。类型std::nullptr_t的prvalue可以转换为任何指针。它是初始化表达式static_cast<std::nullptr_t>(0),它不是空指针值。