为什么编译器不解析对模板函数的调用?

发布于 2024-09-16 12:31:49 字数 387 浏览 3 评论 0原文

在下面的程序中,为什么编译器会为调用 printMax 模板函数而不是调用 printMaxInts 函数生成错误?

#include <iostream>

template<class A>
void printMax(A a,A b)
{
   A c = a>b?a:b;

   std::cout<<c;
}

void printMaxInts(int a ,int b)
{
   int c = a>b?a:b;

   std::cout<<c;

}

int main()
{
   printMax(1,14.45);

   printMaxInts(1,24);
}

In below program why does the compiler generate an error for the call to the printMax template function and not the call to the printMaxInts function?

#include <iostream>

template<class A>
void printMax(A a,A b)
{
   A c = a>b?a:b;

   std::cout<<c;
}

void printMaxInts(int a ,int b)
{
   int c = a>b?a:b;

   std::cout<<c;

}

int main()
{
   printMax(1,14.45);

   printMaxInts(1,24);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昵称有卵用 2024-09-23 12:31:49

为了让编译器从传递给函数模板的参数中推导出模板参数 A,两个参数 ab 都必须具有相同类型。

您的参数的类型为 intdouble,因此编译器无法推断出它应实际用于 A 的类型。 A 应该是 int 还是应该是 double

您可以通过使两个参数具有相同的类型来解决此问题:

printMax(1.0, 14.45);

或者通过显式指定模板参数:

printMax<double>(1, 14.45);

可以调用对非模板函数的调用的原因是编译器不需要推导参数的类型:它知道参数的类型,因为您在函数声明中说了它们的内容:

void printMaxInts(int a, int b)

ab 都是 int 类型。当您将 double 作为参数传递给此函数时,double ->对参数执行 int 标准转换,并使用结果 int 调用该函数。

In order for the compiler to deduce the template parameter A from the arguments passed to the function template, both arguments, a and b must have the same type.

Your arguments are of type int and double, and so the compiler can't deduce what type it should actually use for A. Should A be int or should it be double?

You can fix this by making both arguments have the same type:

printMax(1.0, 14.45);

or by explicitly specifying the template parameter:

printMax<double>(1, 14.45);

The reason that the call to the non-template function can be called is that the compiler does not need to deduce the type of the parameters: it knows the type of the parameters because you said what they were in the function declaration:

void printMaxInts(int a, int b)

Both a and b are of type int. When you pass a double as an argument to this function, the double -> int standard conversion is performed on the argument and the function is called with the resulting int.

爱本泡沫多脆弱 2024-09-23 12:31:49

以下代码基于詹姆斯的答案。您会注意到我已经取出了条件表达式:我这样做是因为该表达式的结果子句必须具有相同的类型,这对 A 和 B 施加了额外的限制。

对 A 和 B 的唯一要求此版本的代码是有一个与它们相关的运算符<()(或者可以将一个运算符转换为另一个),并且存在必需的运算符<<()函数。

template<typename A, typename B>
void printMax(A a, B b)
{    
    if (a < b) 
    {
        cout << b;
    }
    else 
    {
        cout << a;
    }
}

The following code builds on James's answer. You'll notice that I've taken out the conditional expression: I've done this because the result clauses to that expression must have the same type, which imposes an additional restriction on A and B.

The only requirements on A and B in this version of the code is that there's an operator<() that related them (or one can be converted to the other), and that the requisite operator<<() functions exist.

template<typename A, typename B>
void printMax(A a, B b)
{    
    if (a < b) 
    {
        cout << b;
    }
    else 
    {
        cout << a;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文