decltype
is definitely the way to go, but if you must use result_of
, you can do this using function objects defined in <functional>
For example, to get the resulting type double * double
, use
std::result_of<std::multiplies<double>(double, double)>::type
Similarly, unary negation would be
std::result_of<std::negate<int>(int)>::type
With C ++ 14, you can even query the resulting type of mathematical operation on two different types
std::result_of<std::plus<>(double, int)>::type
Of course, the same method can be used for custom types.
struct foo{}; struct bar{}; bar operator/(foo, foo) { return bar{}; } std::result_of<std::divides<>(foo, foo)>::type
Live demo
source share