我正在处理一些 autoconf 的东西,并且有一个测试包括以下内容:
static int test_array [1 - 2 * !((((float)((int)((float)1.4))) == ((float)1.4)) >= 0)];
这失败了:
error: storage size of ‘test_array’ isn’t constant
但是当我将其更改为:
static int test_array [1 - 2 * !((((int)((int)((int)1.4))) == ((int)1.4)) >= 0)];
它工作正常。
我不是很熟悉 autotools 使用的所有技巧,但对我来说,这两种方法的值似乎都应该在编译时确定。
为什么第一个失败了?
最佳答案
让我们看看标准(6.6(6)):
An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants,
sizeof
expressions whose results are integer constants,_Alignof
expressions, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to thesizeof
or_Alignof
operator.
(强调我的)。
您不能在整数常量表达式中强制转换为float
(sizeof
或_Alignof
的参数除外),因此
1 - 2 * !((((float)((int)((float)1.4))) == ((float)1.4)) >= 0)
不是整数常量表达式。
static
数组的元素数必须是求值为正整数的整数常量表达式。
关于c - 涉及 float 的 autoconf 检查的非常量存储大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14200198/