2016-03-31 9 views
0

これは私が見つけて説明した問題の続きですhereです。ビジュアル構造体内でのテンプレートのパラメータ化にconstexprメソッドを使用する

struct ExampleStruct { 
    static constexpr std::size_t Count() noexcept { 
     return 3U; 
    } 
    using Bitset = std::bitset<Count()>; 
}; 

あなたは次のようになります(あなたがconstの式の結果を使用してテンプレートを希望または任意の型) static constexpr機能と std::bitsetの型の別名が含まれている構造体を持っていると言いますStudio 2015バージョン14.0.25029.00 Update 2 RCは Count()コールを赤色で強調表示し、エラー function call must have a constant value in a constant expressionを生成します。

これをコンパイルする方法、または同様の結果を得る方法はありますか?

正確にここでエラーが発生していますか?コンパイラがconst式関数の前に型エイリアスを生成しようとしていますか?

EDIT:これが動作しない理由の説明は以下の見つけることができるが、ここでは、可能な回避策を提供し、誰も私が思いついたものもないので:テンプレートを使用する場合(1)

、店このタイプのエイリアスをに入力します。

template<typename T> 
struct ExampleStruct { 
    using ThisType = ExampleStruct<T>; 
    static constexpr std::size_t Count() noexcept { 
     return 3U; 
    } 
    using Bitset = std::bitset<ThisType::Count()>; 
}; 

(2)構造体の本体の外側Count()機能を移動。

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

struct ExampleStruct { 
    using Bitset = std::bitset<Count()>; 
}; 

(3)constexprメンバ変数を持つconstexpr方法を交換します。

struct ExampleStruct { 
    static constexpr std::size_t Count = 3U; 
    using Bitset = std::bitset<Count>; 
}; 

(4)ストアconstexprメンバ変数の値、及びCount()方法からこれを返します。

struct ExampleStruct { 
private: 
    static constexpr std::size_t m_count = 3U; 
public: 
    static constexpr std::size_t Count() noexcept { 
     return m_count; 
    } 
    using Bitset = std::bitset<m_count>; 
}; 
あなたはクラス本体の外側の一方または両方の線を移動した場合、エラーが消えることに気づいたかもしれません
+0

実際にコンパイルしましたか?インテリセンスに頼らないでください。 – ZDF

+0

'エラーC2975: '_Bits'でエラーをコンパイルしようとしました: 'std :: bitset'のテンプレート引数が正しくない、コンパイル時の定数式' –

+0

'が関数である必要がありますか?なぜ静的なconstexpr std :: size_t Count = 3U; '? – ZDF

答えて

関連する問題