std::declval works, but there is a simpler answer hiding inside ... - access to the real element!
Assuming your Matrix class has an at function like std::vector , you can write
template<typename M> auto operator+(M const & other) -> Matrix<decltype(this->at(0,0) + other.at(0,0))>
Otherwise, replace at with the correct function name, which is used inside the operator+ body to access individual elements.
This has the additional advantage that it works with any other parameter that provides the necessary access function, it should not be another Matrix<T> at all . This is called duck printing, and that is why you should use the same access function that your function body actually uses.
source share