nod*_*dwj 5 c++ templates overloading specialization template-function
从一些关于模板专业化的幻灯片:
#include <iostream>
using namespace std;
template<class X>
X& min(X& a, X& b)
{
return a > b ? b : a;
}
int& min(int& a, int & b)
{
// rewrite of the function in the case of int:
cout << "int explicit function\n";
return a > b ? b : a;
}
/*
new syntax – the more appropriate way:
template<>
int& min<int>(int& a, int& b)
{
cout << "int explicit function\n";
return a > b ? b : a;
}
*/
Run Code Online (Sandbox Code Playgroud)
为什么第二种方式更"合适"?
| 归档时间: |
|
| 查看次数: |
2089 次 |
| 最近记录: |