Zho*_* Ma 7 c++ templates overloading
抱歉标题不清楚,如果找到更好的标题,请随时编辑.在正常功能和模板功能之间的优先级中已经深入讨论了相关主题,但我没有找到我的问题的答案.
我的代码是:
template<typename T>
void f(T t){std::cout << "Template 1" << std::endl;} // template 1
template<typename T, typename B>
void f(T t){std::cout << "Template 2" << std::endl;} // template 2
int main () {
f(1); // line 1, template 1 will be called
f<int>(1); // template 1 will be called
f<int,int>(1); // template 2 will be called
}
Run Code Online (Sandbox Code Playgroud)
在第1行调用模板1函数的可能原因是什么?它在规范中是否定义明确?
在第1行,我认为编译器应该给出"模糊过载"错误.
| 归档时间: |
|
| 查看次数: |
126 次 |
| 最近记录: |