#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