我宣布一个模板Matrix
类是这样的:有条件的模板返回类型在C++
template<typename Type> class Matrix {
// Some code for matrix computations
}
现在,我想超载operator+
在保证了更大的类型将是结果的方式。我一直想把这事
template<typename OtherType>
Matrix<Type> operator+ (Matrix<OtherType> mat) {
// Dimension check and matrix addition code
}
但这样做,我就几乎迫使C++选择Matrix<Type>
作为返回类型。我想实现的是,例如,Matrix<int>
+ Matrix<float>
将导致Matrix<float>
。
有关如何做到这一点的任何建议?
'float's不能存储每个'int'值。 – Yakk