2016-03-28 66 views
0

我試圖鍵入別名std::bitset類,其中模板參數N是使用constexpr函數計算的。但是,這種方法似乎正在陷入困境。調用位集模板參數的constexpr函數

的代碼目前看起來是這樣的:

static constexpr std::size_t ComponentCount() noexcept { 
    return 3U; 
} 

static constexpr std::size_t TagCount() noexcept { 
    return 5U; 
} 

using Bitset = std::bitset<ComponentCount() + TagCount()>; 

而我收到的錯誤是:

1>error C2975: '_Bits': invalid template argument for 'std::bitset', expected compile-time constant expression 
1> note: see declaration of '_Bits' 

感謝您的幫助。

+1

您的代碼使用Visual Studio 2015 Update 1和webcompiler.cloudapp.net編譯。 –

+0

這很奇怪。我也運行Visual Studio 2015(特別是,版本14.0.23107.0)。不過,我不確定這是不是「更新1」。這很可能不會,假設14.1 ...將對應更新1. –

+0

我相信這是RTM。我的版本是14.0.24720.00。在更新1中修復了constexpr錯誤[很多](https://blogs.msdn.microsoft.com/vcblog/2015/12/02/constexpr-in-vs2015-update-1/),因此更新了一個射擊。 –

回答

0

事實證明,我原來的問題沒有包含足夠的上下文。問題最終變得更加微妙。

這裏是我的代碼如何看起來更精確的表示:

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不同的方法。

0

如由@MattWeber的評論指出,使用當前webcompiler.cloudapp.net與編譯器版本19.00.23720.0(內置2016 1月20日)使用您的代碼

int main() 
{ 
    cout << Bitset{}.size() << "\n"; 
} 

並輸出8.因此,只要抓住這個小測試程序最新的Visual Studio並檢查編譯器版本(如果它大於19.00.23720.0它應該工作)。