gcc5.4 不编译以下代码:
// source.cpp
int nonconstexprfunc()
{
return 14;
}
constexpr int func(int n)
{
if (n < 0)
return nonconstexprfunc();
return n*n;
}
int main()
{
constexpr int t1 = func(0);
return 0;
}
我使用的命令:
$ g++ -std=c++14 -c source.cpp
输出:
In function ‘constexpr int func(int)’:
error: ‘constexpr int func(int)’ called in a constant expression
constexpr int t1 = func(0);
In function ‘int main()’:
error: ‘constexpr int func(int)’ called in a constant expression
constexpr int t1 = func(0);
但我可以使用 gcc6.4 编译那个 source.cpp。gcc5.4 不完全支持 constexpr 函数吗?
更有趣的是,我可以使用使用 gcc5.4 的 icpc(英特尔 C++ 编译器)编译该 source.cpp - 我想必须有一个选项可以使用 gcc5.4 编译该代码。
$ icpc -v
icpc version 19.0 (gcc version 5.4.0 compatibility)
$ icpc -std=c++14 -c source.cpp
no errors