2011-10-17 20 views
2
template<class Int_T,class Integral,typename Best_Fit<Int_T>::type Min_Range, 
    typename Best_Fit<Int_T>::type Max_Range> 
auto operator+(Integral left,const Int<Int_T,Min_Range,Max_Range>& right) 
    ->Int<decltype(left + right.get_data())> 
{ 
    static_assert(std::is_integral<Integral>::value,"Non integral type is not allowed."); 
    static_assert(std::is_integral<Int_T>::value,"Non integral type is not allowed."); 
    auto data = left + right.get_data(); 
    Int<decltype(left + right.get_data())> result(data); 
    return result; 
} 

問題是,我不認爲這兩個static_assert * *會被觸發 - 即使有人也嘗試。
那麼這個Q的答案是什麼?這個static_assert會被觸發嗎?

+0

什麼是'Int'? –

+0

我不明白爲什麼不這樣做,你*可以*提出一個滿足約束的類('Best_Fit'和'Int' - 無論它們是什麼),但是失敗is_integral ... – Nim

+3

我認爲這是不可能的回答沒有'Int <>','Best_Fit <>'的定義,因爲其中任何一個都可能觸發SFINAE – sehe

回答

0
std::string x; 
Int<int, 3, 5> i; 

auto z = x + i; 

觸發它。

+0

這很奇怪。我希望'decltype(left + right.get_data())'到SFINAE out'operator +'。事實上,我無法在我的GCC副本上重現您的發現。 –

+0

好吧,嘗試任何可轉換或可加入到'int'的類型,甚至SFINAE也不會幫助你。例如。 'double'。 – Ayjay

+0

'std :: string'不是這樣的類型。 –

1
struct dummy { 
    operator int() const 
    { return 0; } 
}; 

// Where rhs has appropriate type 
dummy() + rhs; 
相關問題