【发布时间】:2017-03-19 06:43:20
【问题描述】:
玩具代码很简单:
#include <stdio.h>
#include <math.h>
#define A 10
#define SIZE (int)(ceil(A / 2)) // needs computation but known at compile-time
struct node {
int keys[SIZE];
};
int main() {
node a_node;
for (int i = 0; i < SIZE; ++i) {
a_node.keys[i] = i;
printf("%d\n", a_node.keys[i]);
}
return 0;
}
当我使用g++ -std=c++11 -O3 test.cpp -o test 编译它时,它按预期通过并运行;但是使用带有icpc -std=c++11 -O3 test.cpp -o test的intel编译器,会出现错误:
test.cpp(8): error: function call must have a constant value in a constant expression
int keys[SIZE];
为什么将其视为函数调用?但是如果我在宏中使用简单的算术(例如#define SIZE (A / 2)),错误就会消失。
gcc 版本:4.8.5; ICC版本:16.0.3。
任何线索如何解决这个问题?谢谢。
----------------- 已添加------------------
如果 int keys[SIZE] 在主函数而不是结构中声明,即像这样:
#include <stdio.h>
#include <math.h>
#define A 10
#define SIZE (int)(ceil(A / 2))
/*struct node {
int keys[SIZE];
};*/
int main() {
//node a_node;
int keys[SIZE];
for (int i = 0; i < SIZE; ++i) {
keys[i] = i;
printf("%d\n", keys[i]);
}
return 0;
}
它将通过 gnu 和 intel 编译器。很有趣也很奇怪。我不确定我是否犯了任何我不知道的错误,任何可能的线索?
【问题讨论】:
-
你是用 C 还是 C++ 编码?
c++标签是可疑的。