“初始化器不是编译时间常数".错误在.mm中起作用? [英] "Initializer is not a compile time constant" error works in .mm?
问题描述
我对正在观察的情况感到困惑,并希望获得一些见识.首先,我使用Xcode 5,并将LLVM 5编译器选项设置为默认值.
I'm puzzled by a situation I'm observing and would love some insight. First, I'm using Xcode 5, with LLVM 5 compiler options set to defaults.
.m文件中有一行,例如:
I have a line in a .m file such as:
static NSArray * const kSchemaVersions = @[@"1"];
而且,正如预期的那样,我看到一个编译器错误,提示Initializer element is not a compile-time constant.
And, as expected, I see a compiler error saying Initializer element is not a compile-time constant.
但是,如果我将同一行放在.mm
(Objective C ++)文件中,则编译器不会抱怨.
However, if I place this same line in a .mm
(Objective C++) file, the compiler does not complain.
我完全理解为什么它不应该工作,但是我对为什么它看起来困惑不解.
I completely understand why it shouldn't work, but I'm baffled as to why it appears to.
有想法吗?
推荐答案
正如您提到的,在C和Objective-C中,静态变量只能使用编译时常量进行初始化.另一方面,在C ++(因此是Objective-C ++)中,在运行main
之前,在运行时分配了静态变量.
As you mentioned, in C and Objective-C static variables can only be initialised with compile-time constants. In C++ (and therefore Objective-C++) on the other hand, static variables are assigned at run time, before main
runs.
有关更多详细信息,请参阅Eli Bendersky的 C和C ++中的非常数全局初始化
For more details have a look at Eli Bendersky's Non-constant global initialization in C and C++
这篇关于“初始化器不是编译时间常数".错误在.mm中起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!