Use std::declval<T>();
(C++11):
#include <utility>
template <typename CompatibleType>
auto operator+(Matrix<CompatibleType> const & other)
-> Matrix<decltype(std::declval<ValueType>() + std::declval<CompatibleType>())>
{ /* ... */ }
std::declval
returns an rvalue-reference, and will only work in an unevaluated-context, which decltype
happens to be.
If your compiler doesn't support this Standard, use this pointer trick (which also only works in an unevaluated-context):
-> Matrix<decltype(*(ValueType*)(0) + *(CompatibleType*)(0))>
// or
-> Matrix<decltype(*static_cast<ValueType*>(0) +
*static_cast<CompatibleType*>(0))>