Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8813189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:47:00+00:00 2026-06-14T03:47:00+00:00

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

  • 0
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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:47:02+00:00Added an answer on June 14, 2026 at 3:47 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have non-template class with a templatized constructor. This code compiles for me. But
Look at this template: template <class T> auto checkErrorCode(T& functor) -> decltype(functor()) { auto
struct X{}; template<class T> decltype(X() == int()) f(T const&){ return true; } int main(void)
template<class T> std::vector<T> convert(int argument) { } int main() { decltype(&convert<int>); return 0; }
This answer has a code snippet like this : template<class T, class F> auto
Say I have a template class that takes msgs from source, does something smart
Im working on a template class which represents a managed Array. E (*data)[]; data
I am writing a template class which takes a floating-point-like type (float, double, decimal,
Possible Duplicate: trailing return type using decltype with a variadic template function I'm getting
Possible Duplicate: trailing return type using decltype with a variadic template function I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.