2016-12-29 22 views
0

我想實現一個表達式類2算術類型的專業化。這是默認的類:模板專門化與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 { /* ... */ } 

而這些都是兩個專業:

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

您有多個主類模板,並且這些模板不能被替換。您需要有一個主要模板,然後是多個專業化。一個簡單的方法是採取不同的方式:

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

謝謝你的回答。多重主要案件的問題是這個問題! –