#include <algorithm>
struct S
{
static constexpr int X = 10;
};
int main()
{
return std::min(S::X, 0);
};
如果std::min
需要一个const int&
,编译器很可能想有S::X
还定义了某个地方,即S::X
必须的存储器中存在。力constexpr在编译时进行评估
有没有办法force编译器在编译时评估我的constexpr
?
的原因是:
最初,我们在init优先静态变量的初始化初出现了问题。有一些struct Type<int> { static int max; };
,和一些全球static int x = Type<int>::max;
,和一些其他早期代码other_init
使用,即x
。当我们更新GCC时,突然我们在other_init
中有x == 0
。
我们认为我们可以通过使用constexpr
来避免这个问题,因此它总会在编译时评估它。
唯一的另一种方法是使用struct Type<int> { static constexpr int max(); };
来代替,即让它成为函数。
'constexpr'不能解决静态初始化订购Fiasco。 – chris
你可以发布给你的代码吗?我的意思是结构类型为的那个。我有兴趣看到它。 –
@MarcoA .:你的意思是什么代码?它已经在那里。让'other_init'成为__attribute __((构造函数))',然后'printf(“%i”,x);'。 – Albert