クラスにはpush
の2つの異なる実装があり、ブール値のテンプレート引数に基づいて選択します。そのように、this answerで説明したように、私はSFINAE原理を使用してみました:booleanテンプレートパラメータを使用してメンバ関数を有効にするにはどうすればよいですか?
template<class T, bool foo=true>
class Bar {
template <>
typename std::enable_if<foo>::type
push(const T& value) { /* one implementation */}
template <>
typename std::enable_if<!foo>::type
push(const T& value) { /* another implementation */ }
}
しかし、私はgccの下の「クラススコープ内の関数push
を特化することはできません」のエラーを取得しています、と私はその理由を理解していません。私のコードは、リンクされた答えのそれとまったく同じではありませんが、非常によく似ていると私は重要な違いを見つけることができません。
私もthis answerで提案されているものに似た構文を使用してみましたが、それはまた、動作しない(エラーではなく、「クラスのメンバは、再宣言することはできません」さ):
template <bool enable=foo>
typename std::enable_if<enable>::type
push(const T& value) { /* one implementation */}
template <bool enable=!foo>
typename std::enable_if<enable>::type
push(const T& value) { /* another implementation */ }
は、どのように私はこれを達成することができますか?
うわー、それは完全に機能します。ありがとうございました。 –