为什么我可以修改常量__restrict指针,但不能修改 typdef'd 版本?



注意:我使用的是最新版本Xcode附带的objective C编译器。

为什么这是合法的:

void verySpecial(const float* __restrict foo, const int size) {
    for (int i = 0; i < size; ++i) {
        // ... do special things ...
        ++foo;  // <-- Should be illegal to modify const pointer?
    }
}

但是,如果我使用typedef,它会做我认为它应该做的事情。

typedef float* __restrict RFloatPtr;
void verySpecial(const RFloatPtr foo, const int size) {
    for (int i = 0; i < size; ++i) {
        // ... do special things ...
        ++foo;  // <-- Now this is a compiler error.
    }
}

那么,在类型定义的情况下有什么不同,我不明白的是什么?阅读__restrict使我的大脑受伤,我甚至不确定它在这里是否重要。

++foo;  // <-- Should be illegal to modify const pointer?

狂吠。修改const指针是非法的。然而,将非const指针修改为const指针则不是。我想你搞混了

const float *foo

float *const foo

当然,你也不能修改一个restrict指针,因为它没有意义。restrict告诉编译器该指针保证不与其他指针重叠。如果对指针进行自减或自增操作,则该假设可能不再成立。

最新更新