0

环境

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

产生一个

错误:初始化元素不是编译时常量 NMFooStruct const NMFooStructConst = { .fooBlock = NMFooBlockConst };

即使 NMFooBlockConst 被定义为 const,这是预期的行为吗?

4

2 回答 2

1

这个答案来自 mikeash。

'NMFooBlockConst' 不是编译时常量表达式,因此是不允许的。

尽管表达式 ^{} 是编译时常量,但 'NMFooBlockConst' 并不符合语言定义。

根据定义,变量不是编译时常量表达式。^{} 是。

关键字 const 是不相关的。

const 关键字与某个东西是否是编译时常量表达式无关。

赞赏迈克。

于 2013-04-17T23:22:18.300 回答
1

答案就在这里,我认为:https ://stackoverflow.com/a/6143271/73479

但是,这将起作用:

NMFooStruct const NMFooStructConst = { .fooBlock = ^{} };
于 2013-04-17T21:23:06.353 回答