简体   繁体   English

奇怪的模板错误:错误C2783:无法推断模板参数

[英]Strange Template error : error C2783: could not deduce template argument

I have created a simple function with 2 diffrernt template arguments t1, t2 and return type t3. 我创建了一个带有2个diffrernt模板参数t1,t2和返回类型t3的简单函数。 So far no compilation error. 到目前为止没有编译错误。 But when Itry to call the function from main, I encounter error C2783. 但是当Itry从main调用函数时,我遇到错误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;
}

There is no way for the compiler to deduce t3 from the function arguments. 编译器无法从函数参数中推导出t3 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) . 然后你可以用adder1<int>(1, 6.0)来调用它。 It's more difficult if you want to deduce t3 to the actual result of the addition. 如果你想将t3推导到实际的加法结果,那就更难了。 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 C ++ 0x(下一个C ++版本的代号)将允许通过以下方式表示返回类型等于添加类型来执行此操作

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. 在当前的C ++版本中模拟这并不容易。 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" Herb Sutter所说: “写下你所知道的,知道你写的是什么”

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);
    };

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 . 如果你知道返回类型是int ,那么把它变成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;
}

In your case the only way to call your function would be adder1<int, double, int>(...) . 在你的情况下,调用你的函数的唯一方法是adder1<int, double, int>(...)

You could make your function return an explicit t3 argument or pass this argument by a reference, like 你可以让你的函数返回一个显式的t3参数或者通过引用传递这个参数,比如

adder(const t1& a, const t2&b, t3& result)

You are always returning an int hence t3 is not required. 你总是返回一个int因此不需要t3。 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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM