Is it possible to overload operator +
template <typename U>
Mat <T> operator + ( const Mat <U> &A );
for the following matrix operations
Matrix (1,1) + scalar = scalar
scalar + Matrix (1,1) = scalar
Matrix(1,1) + Matrix(1,1) = scalar
and a syntax (or alike)?
template <typename T, typename U>
T operator + ( const Mat <U> &M ) const;
Updated question:
The dimension as a template parameter:
template <typename T, typename U, const unsigned int TDim>
T operator + ( const Mat <U> &M, TDim <dim> ) const;
with the specialization for dim=1
template <typename T, typename U>
T operator + ( const Mat <U> &M, TDim <1> ) const;
Not unless the dimensions of the matrix are made into template parameters of
Mat, and you partially specialize theMattemplate for1x1matrices.Alternatively, you could return
Matrix(1,1)and then have a conversion to scalar. If you did this, you’d have to accept the possibility of the conversion failing at runtime.