Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusing syntax with anonymous template types?

Tags:

c++

c++11

template <class T, class U> decltype(*(T*)(0) * *(U*)(0)) mul(T x, U y) {
   return x * y;
}

This piece of code was taken from Stroustrup's C++11 FAQ. I understand what it does, which is multiply two objects of varying types. What perplexes me is the syntax between the template parameters and the function definition. What is happening inside decltype? I take it that it's dereferencing an unnammed T pointer initialized to 0, and multiplying it by an unnamed U pointer being dereferenced and initialized in the same way. Am I right?

Well, if this is what is happening, then isn't the use of pointers, dereferences, and extra parenthesis superfluous? Couldn't I initialize the types like this while maintaining the desired effect?:

template <class T, class U> decltype(T(0) * U(0)) mul(T x, U y) {
   return x * y;
}

This looks much cleaner to me, and it does have the same effect when multiplying two numbers like in the first...

mul(4, 3); // 12

So why does Stroustrup insist on using complex pointer, dereference and initialization syntax? This is, of course, before he introduced the new auto syntax. But anyway, my question is: Is there any difference between the two above forms of type initialization? Where he uses pointers and instantly dereferences them instead of simply doing what I did, which was to initialize the types with no pointers or dereferencing? Any response is appreciated.

like image 851
David G Avatar asked Nov 10 '12 13:11

David G


1 Answers

Your version is not equivalent.

  1. Your version supposes that both T and U can be constructed from 0. It's obviously wrong to expect this from matrices, yet they can be multiplied.
  2. T(0) yields a temporary (that may bind to T&&) whilst *(T*(0)) yields a reference to an existing object (that is, T&), therefore a different operator might be selected.

However, neither Stroustrup's version nor yours end up being used in practice. At an equivalent level of compiler implementation, one would use:

template <typename T, typename U>
decltype(std::declval<T>() * std::declval<U>()) mul(T x, U y);

But it's failing to take advantage of Late Return Type specification, built to allow postponing the declaration of the return type after the function's arguments declaration: auto f(int, int) -> int. Once the arguments have been declared, they can be used, which is incredibly useful for decltype!

template <typename T, typename U>
auto mul(T x, U y) -> decltype(x * y);

This latter form is guaranteed to pick the same operator overload than the function body, at the cost of repetition (unfortunately).

like image 53
Matthieu M. Avatar answered Oct 07 '22 17:10

Matthieu M.