Return double or complex <double> from a template function

I am writing some function templates to overload the * operator for a matrix class. I work a lot with matrices like double and complex<double> . Is it possible to write a single template function that returns the correct type? For instance:

 template<class T, class U, class V> matrix<V> operator*(const T a, const matrix<U> A) { matrix<V> B(A.size(1),A.size(2)); for(int ii = 0; ii < B.size(1); ii++) { for(int jj = 0; jj < B.size(2); jj++) { B(ii,jj) = a*A(ii,jj); } } return B; } 

I would like the return type V be determined by the natural result of T*U Is it possible?

EDIT:

The next question I answered received answers that provide additional information applicable here.

+4
source share
1 answer

In C ++ 11, you can use the syntax for declaring an alternative function:

 #include <utility> // for declval template<class T, class U, class V> auto operator*(const T a, const matrix<U> A) -> decltype( std::declval<T>() * std::declval<U>() ) { //... } 
+4
source

Source: https://habr.com/ru/post/1493543/


All Articles