2016-12-29 13 views
0

算術型の2つの特殊化を使用してExpressionクラスを実装しようとしています。これはデフォルトクラスです:クラスのenable_ifおよびis_arithmeticを使用したテンプレートの特殊化

template<typename Left, typename Op, typename Right, typename std::enable_if<!std::is_arithmetic<Left>::value, Left>::type* = nullptr> 
class Expression { /* ... */ } 

そして、それらの2つの専門分野です:私は今、私のコードをコンパイルする場合

template<typename Left, typename Op, typename Right, typename std::enable_if<std::is_arithmetic<Left>::value, Left>::type* = nullptr> 
class Expression { /* ... */ }; 

template<typename Left, typename Op, typename Right, typename std::enable_if<std::is_arithmetic<Right>::value, Right>::type* = nullptr> 
class Expression { /* ... */ }; 

私はこのエラーを取得する:

Error C3855 'Expression': template parameter '__formal' is incompatible with the declaration Vector

どのように私は私を解決することができます私が使ったテンプレートや特殊化やダミータイプの問題。

+0

同じエラーを示す最小限のコード例を共有してください。あなたが提供した情報で、推測するのは難しいですが、何が間違っています。 – paweldac

+1

私は '__formal'(予約された識別子です)という名前のテンプレートパラメータ、または' Vector'という名前の宣言をサンプルコードのどこにも表示しません。 [mcve]を投稿してください。 – Praetorian

答えて

2

複数のプライマリクラステンプレートがあり、置き換えることはできません。 1つのプライマリテンプレートと複数のスペシャライゼーションが必要です。単純なアプローチが異なり、それを行うことです。

template<typename Left, typename Op, typename Right, 
     int = std::is_arithmetic_v<Left> + 2 * std::is_arithmetic_v<Right>> 
class Expression; 

template <typename Left, typename Op, typename Right> 
class Expression<Left, Op, Right, 0> { 
    // base case 
}; 
template <typename Left, typename Op, typename Right> 
class Expression<Left, Op, Right, 1> { 
    // only the left operand is arithmetic 
}; 
template <typename Left, typename Op, typename Right> 
class Expression<Left, Op, Right, 2> { 
    // only the right operand is arithmetic 
}; 
template <typename Left, typename Op, typename Right> 
class Expression<Left, Op, Right, 3> { 
    // both operands are arithmetic 
}; 

あなたが一緒に処理することができ、複数のケースを持っている場合は、これらの主要なテンプレートを作成し、唯一残っている特殊なケースを専門にすることができます。

+0

ありがとうございます。複数の主なケースの問題が問題でした。 –

関連する問題