Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Confusing syntax with anonymous template types?

    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 [1]. 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 has the same effect with multiplying as the first...

    mul(4, 3); // 12

So why does Stroustrup insist on using the complexity of pointers, dereferencing them and initializating them like he does? This is, of course, before he introduced the new auto syntax [2]. 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.

    [1]: http://www.stroustrup.com/C++11FAQ.html#suffix-ret...

    [2]: http://en.wikipedia.org/wiki/C%2B%2B11#Alternative...

1 Answer

Relevance
Still have questions? Get your answers by asking now.