正常功能和模板功能之间的优先级 [英] Priority between normal function and Template function

查看:124
本文介绍了正常功能和模板功能之间的优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,main函数使用普通函数代替Template函数。

In the following code, the main function uses normal function instead of Template function.

#include <iostream>

using namespace std;

template <class T>
void num(T t){cout<<"T : "<<t;}

void num(int a){cout<<"wT : "<<a;}


int main()
{
    num(5);
    return 0;
}

这可能是什么原因?

推荐答案

在这种情况下,为了调用模板方法,需要使用 num< int>(5)而不是 num(5)。虽然编译器可以推断,非泛型方法优先于通用方法。您可以在这里 http://ideone.com/ccDJP 查看此行为。

To call the template method in this case, you need to invoke the method explicitly with num<int>(5) instead of num(5). Although the compiler can infer, non-generic method is preferred to a generic one. You can take a look at this behavior here http://ideone.com/ccDJP.

这篇关于正常功能和模板功能之间的优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆