I am reading this question on isocpp FAQ here, this question is explaining how to write the return type for ???
template<class T, class U>
??? mul(T x, U y)
{
return x*y;
}
I understand the easy way is to write auto mul(T x, U y) -> decltype(x*y)
, however the question also gives another way, which is to replace ???
by decltype(*(T*)(0)**(U*)(0))
. But I don't fully understand what this decltype(*(T*)(0)**(U*)(0))
is really doing, it seems that it is declaring a temporary pointer T*
and initialize it to zero and then dereference the pointer, then multiplied by the same counterpart for type U
, is my understanding right?
But why using pointers? I think decltype(T(0)*U(0))
or decltype(T{0}*U{0})
should also work.