2013-04-17 87 views
0

注意:我正在使用Xcode最新版本附帶的目標C編譯器。爲什麼我可以修改const __restrict指針而不是typdef'd版本?

爲什麼說這是合法的:

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. 
    } 
} 

那麼,什麼是在typedef定義不同的情況下,什麼不我明白?閱讀__restrict會讓我的大腦受到傷害,我甚至不確定這是否重要。

+1

First Apple's沒有使用gcc或llvm的objective-c編譯器。 –

+1

@AnoopVaidya哼,什麼? – 2013-04-17 17:50:07

+0

無論如何。這是XCode附帶的編譯器。應用商店中最新版本的XCode。 –

回答

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

Yap。修改一個const指針是非法的。但是,將非const指針修改爲const不是。我想你混淆

const float *foo 

float *const foo 

而且,你當然不能修改restrict指針,因爲它沒有任何意義。 restrict告訴編譯器指針保證不與其他指針重疊。如果您減少或增加指針,則此假設可能不再成立。

相關問題