事實證明,我原來的問題沒有包含足夠的上下文。問題最終變得更加微妙。
這裏是我的代碼如何看起來更精確的表示:
template
<
typename ComponentList,
typename TagList,
typename SignatureList
>
struct Settings {
// ...
static constexpr std::size_t ComponentCount() noexcept {
return 3U;
}
static constexpr std::size_t TagCount() noexcept {
return 5U;
}
// ...
using Bitset = std::bitset<ComponentCount() + TagCount()>;
// ...
};
這種做法似乎還好我,並沒有給我提供任何編譯器警告或任何東西。只是原始問題中提到的編譯器錯誤。
然而,當我試圖以更準確地隔離問題進一步簡化的問題,我結束了這一點:
struct Settings {
static constexpr std::size_t ComponentCount() noexcept {
return 3U;
}
static constexpr std::size_t TagCount() noexcept {
return 5U;
}
using Bitset = std::bitset<ComponentCount() + TagCount()>;
};
這樣簡化之後(或更具體地,去除模板參數之後) ,VS2015在ComponentCount()
和TagCount()
函數調用中找到了錯誤function call must have a constant value in a constant expression
,並用紅色突出顯示它們。顯然,編譯器無法查看與常量表達式包含在同一個結構中的靜態constexpr函數?奇怪的。它可能是在定義常量表達式之前嘗試執行類型別名。
的模板結構的解決方案如下:
using ThisType = Settings<ComponentList, TagList, SignatureList>;
// ...
using Bitset = std::bitset<ThisType::ComponentCount() + ThisType::TagCount()>;
然而,這種方法不適用於非模板結構的工作。在這種情況下查看我的其他StackOverflow post不同的方法。
您的代碼使用Visual Studio 2015 Update 1和webcompiler.cloudapp.net編譯。 –
這很奇怪。我也運行Visual Studio 2015(特別是,版本14.0.23107.0)。不過,我不確定這是不是「更新1」。這很可能不會,假設14.1 ...將對應更新1. –
我相信這是RTM。我的版本是14.0.24720.00。在更新1中修復了constexpr錯誤[很多](https://blogs.msdn.microsoft.com/vcblog/2015/12/02/constexpr-in-vs2015-update-1/),因此更新了一個射擊。 –