2013-04-17 53 views
0

環境初始化器元件是不是編譯時間常數 - 從一個const結構參照一個const塊

的XCode 4.6.2
OSX 10.7.5

NMFoo.h

typedef void(^NMFooBlock)(); 

struct NMFooStruct { 
    __unsafe_unretained NMFooBlock fooBlock; 
}; 
typedef struct NMFooStruct NMFooStruct; 

@interface NMFoo : NSObject 

@end 

NMFoo.m

#import "NMFoo.h" 

NMFooBlock const NMFooBlockConst = ^{}; 

NMFooStruct const NMFooStructConst = { .fooBlock = NMFooBlockConst }; 

@implementation NMFoo 

@end 

產生一個

error: initializer element is not a compile-time constant NMFooStruct const NMFooStructConst = { .fooBlock = NMFooBlockConst };

那是預期的行爲,即使NMFooBlockConst被定義爲const?

+0

它肯定是1.不是「聯繫」,而是常數,2.這與Xcode無關。 – 2013-04-17 17:15:07

回答

1

This answer came from mikeash.

'NMFooBlockConst' isn't a compile-time constant expression, so it's not allowed.

即使表達^ {}是一個編譯時常數,'NMFooBlockConst'不符合語言定義。

Variables aren't compile-time constant expressions, by definition. ^{} is.

關鍵字const是無關的。

The const keyword has nothing to do with whether something is a compile-time constant expression.

讚賞邁克。

相關問題