You should use decltype
and std::declval<>
as:
template<typename T1, typename T2>
auto DoSomething(const T1& arg) -> decltype(arg * std::declval<T2>())
{
T2 t2Obj; // Or have it obtained in some other way
return arg * t2Obj;
}
because T2
might not have default constructor, so decltype(arg *T2())
may not work.
But then I also notice that if T2
doesn't have default constructor, then you would not be able to write T2 t2Obj
either. So if you require T2
to have default constructor, then you can simply write this:
-> decltype(arg *T2()) //requirement : T2 must have default constructor
that should also work if you require T2
to have default constructor!