__attribute __((const))是否适用于Clang的Objective-C方法?

Eon*_*nil 3 attributes objective-c clang

__attribute__((const))是一个GCC归属,用于检查函数执行的纯度.我认为Clang支持这个,但是当我把它放到一个方法上时,它似乎不起作用.

@interface C1
- (id)method1 __attribute__((const));
@end


int a = 0;

@implementation C1
- (id)method1 __attribute__((const))
{
    a++;
    return nil;
}
@end
Run Code Online (Sandbox Code Playgroud)

上面的代码不会产生任何警告或错误.

这个归属是否适用于Clang?或者我该怎么做才能让它发挥作用?

rob*_*off 8

const属性不检查函数的纯度.它声明了函数的纯度,因此优化器可以消除对函数的调用.你真的要确保这个功能是纯粹的.