该标准说取消引用空指针会导致未定义的行为。但什么是“空指针”?在下面的代码中,我们称之为“空指针”:
struct X
{
static X* get() { return reinterpret_cast<X*>(1); }
void f() { }
};
int main()
{
X* x = 0;
(*x).f(); // the null pointer? (1)
x = X::get();
(*x).f(); // the null pointer? (2)
x = reinterpret_cast<X*>( X::get() - X::get() );
(*x).f(); // the null pointer? (3)
(*(X*)0).f(); // I think that this the only null pointer here (4)
}
我的想法是,空指针的取消引用仅在最后一种情况下发生。我对吗?根据 C++ 标准,编译时空指针和运行时之间有区别吗?