是否由C++标准保证,如果我有两个相同类型的指针,其值等于nullptr,那些指针之间的差异等于0?
在伪数学符号中,以下谓词是否成立?
ForAll x ForAll y(x == nullptr)^(y == nullptr) - >(x - y == 0)
我能想到的最简单的代码示例:
int* x = nullptr;
int* y = nullptr;
assert(x - y == 0);
Run Code Online (Sandbox Code Playgroud)
我想这可以归结为:是否有可能有一个有效的C++标准实现,其中有多个位表示nullptr只能比较为相等,因为相等运算符做了一些魔术?
由于指针算术是在同一数组中定义的,我怀疑是否可以NULL从另一个数组中减去NULL。我担心的是:
//first and second can both either be from the same array
//or be both NULL
prtdiff_t sub(void *first, void *second){
//Do I really need this condition?
if(!first && !second)
return (ptrdiff_t) 0;
return second - first;
}
Run Code Online (Sandbox Code Playgroud)