tags:

views:

132

answers:

4

Hi, 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;
}
A: 

In your case the only way to call your function would be adder1<int, double, int>(...).

You could make your function return an explicit t3 argument or pass this argument by a reference, like

adder(const t1& a, const t2&b, t3& result)
Kotti
+7  A: 

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);
    };
Johannes Schaub - litb
`std::plus<t>` anyone? :) +1
Billy ONeal
@Johannes many thanks for the reply
osum
A: 

You are always returning an int hence t3 is not required. You can modify your code as:

template <typename t1, typename t2> 
int 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;

}
Naveen
+3  A: 

When attempting to deduce the template type, the compiler is not going to look at the actual code of the function. If you know the return type will be int, then make it int.

template <typename t1, typename t2> 
int 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;
}
Billy ONeal