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

  • Home
  • SEARCH
  • 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 8912805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:17:58+00:00 2026-06-15T04:17:58+00:00

Consider this code : #include <iostream> #include <typeinfo> using namespace std; template<typename T1, typename

  • 0

Consider this code :

#include <iostream>
#include <typeinfo>

using namespace std;

template<typename T1, typename T2>
auto add(T1 l, T2 r) -> decltype(l + r){
    return l + r;
}

class C {};

class B {};

class A {
public:
    C operator+(const B& b) {
        C c;
        return c;
    }
};


int main() {
    // Using add()
    A a;
    B b;
    auto c = add(a, b);

    cout << typeid(a).name() << endl;
    cout << typeid(b).name() << endl;
    cout << typeid(c).name() << endl;
    cout << endl;

    // Doing the same thing but not on a function
    A a2;
    B b2;
    auto c2 = a2 + b2;

    cout << typeid(a2).name() << endl;
    cout << typeid(b2).name() << endl;
    cout << typeid(c2).name() << endl;
}

I just have a very simple question: why do I need to put decltype() in the postfix return type of add() unlike in the second method (the one that doesn’t use add())?

  • 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-15T04:18:00+00:00Added an answer on June 15, 2026 at 4:18 am

    why do I need to put decltype() in the postfix return type of add() unlike in the second method (the one that doesn’t use add())?

    Because that’s part of the rules of C++. The parser is run left-to-right; for the most part, if an identifier hasn’t been reached yet, the parser doesn’t know about it. Identifiers like the function parameters.

    So if you use an expression to determine the type of the return value, and that expression uses the parameters in some way, you must put the return type after the function arguments.

    But my point is, why can’t the compiler automatically deduce the return type when it knows that A + B returns C?

    Because those are the rules of C++: a function must have a return type specified directly in the function declaration. The compiler isn’t allowed to deduce it.

    Yet.

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

Sidebar

Related Questions

Consider this code: #include <iostream> #include <type_traits> using namespace std; template<typename T_orig> void f(T_orig&
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
Consider this code: #include <iostream> using namespace std; typedef int array[12]; array sample; array
Consider this code #include <iostream> #include <cstdio> using namespace std; class Dummy { public:
Please consider this code: #include <iostream> template<typename T> void f(T x) { std::cout <<
Consider the following code: #include<iostream> #include<vector> using namespace std; class Foo { public: template<
Consider this piece of code: #include <vector> #include <iostream> using namespace std; class Base
consider the following c++ code #include stdafx.h #include<iostream> using namespace std; this much part
Consider this C++ code: #include <iostream> using namespace std; struct B { virtual int
Consider the following code: #include <iostream> #include <functional> using namespace std; template<class T> void

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.