相关疑难解决方法(0)

减去两个nullptr值保证为零?

是否由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只能比较为相等,因为相等运算符做了一些魔术?

c++ c++11

5
推荐指数
1
解决办法
458
查看次数

我们可以减去NULL指针吗?

由于指针算术是在同一数组中定义的,我怀疑是否可以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)

c pointer-arithmetic null-pointer ptrdiff-t

5
推荐指数
2
解决办法
171
查看次数

标签 统计

c ×1

c++ ×1

c++11 ×1

null-pointer ×1

pointer-arithmetic ×1

ptrdiff-t ×1