这个static_assert会被触发吗?

时间:2011-10-17 10:19:47

标签: c++ c++11 static-assert

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 * s * - 即使有人也会尝试。
那么Q的答案是什么?

2 个答案:

答案 0 :(得分:1)

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

// Where rhs has appropriate type
dummy() + rhs;

答案 1 :(得分:0)

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

auto z = x + i;

触发它。