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 810833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:56:53+00:00 2026-05-15T00:56:53+00:00

I have created a simple function with 2 diffrernt template arguments t1, t2 and

  • 0

I have created a simple function with 2 diffrernt template arguments t1, t2 and return type t3.
So far no compilation error. But when Itry to call the function from main, I encounter error C2783.
I needed to know If the following code is legally ok? If not how is it fixed?
please help!

template <typename t1, typename t2, typename t3> 
t3 adder1 (t1  a , t2 b)
    {
        return int(a + b);
    };


int main()
{
       int sum = adder1(1,6.0);  // error C2783 could not deduce template argument for t3
       return 0;
}
  • 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-05-15T00:56:54+00:00Added an answer on May 15, 2026 at 12:56 am

    There is no way for the compiler to deduce t3 from the function arguments. You need to pass this argument explicitly. Change the order of the parameters to make this possible

    template <typename t3, typename t1, typename t2> 
    t3 adder1 (t1  a , t2 b)
        {
            return t3(a + b); // use t3 instead of fixed "int" here!
        };
    

    Then you can call it with adder1<int>(1, 6.0). It’s more difficult if you want to deduce t3 to the actual result of the addition. C++0x (next C++ version’s codename) will allow to do this by saying that the return type is equal to the type of the addition the following way

    template <typename t1, typename t2> 
    auto adder1 (t1  a , t2 b) -> decltype(a+b)
        {
            return a + b;
        };
    

    Then you could cast explictly at the point of use

    int sum = (int) adder1(1,6.0); // cast from double to int
    

    Simulate this in the current C++ version isn’t going to be easy. You can use my promote template to do that. If you feel that this rather confuses the matter for you and that you are OK with explictly providing the return type, i think it’s better to stay with explicitly providing it. Like Herb Sutter says “Write What You Know, and Know What You Write”

    Nontheless you can do the above like this with that template

    template <typename t1, typename t2> 
    typename promote<t1, t2>::type adder1 (t1 a, t2 b)
        {
            return (a + b);
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 437k
  • Answers 437k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer With newer versions of MATLAB loops aren't as costly as… May 15, 2026 at 4:16 pm
  • Editorial Team
    Editorial Team added an answer protobuf (under any implementation) is not an RPC stack; it… May 15, 2026 at 4:16 pm
  • Editorial Team
    Editorial Team added an answer It's not actually iCal, but you probably mean the Event… May 15, 2026 at 4:16 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.