// test_templates.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
template<class U>
class Y
{
public:
template<class V> void f(U,V);
void g(U);
template<> void f<int>(U,int);
};
template<class U> template<class V>
void Y<U>::f(U, V) { cout << "Template 1" << endl; }
template<> template<>
void Y<int>::f<int>(int, int) { cout << "Template 5" << endl; }
template<> template<class V>
void Y<int>::f(int, V) { cout << "Template 4" << endl; }
template<> template<>
void Y<int>::f<int>(int,int)
{
cout<<"my temp"<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
Y<int> a;
Y<char> b;
a.f(1, 2);
a.f(3, 'x');
b.f('x', 'y');
return 0;
}
看这个我改动了IMB上测试例子的,我得到了几点:
首先模板主函数无特例的的时候(少红色部分),编译可以通过,模板是按照顺序显示的,第一个显示的是先出现的特例模板之一,
如果加上红色部分,那么显示就是根据类内函数顺序来的,特例被最后那个匹配~。~ ,其实两个特例的函数完全是相同的,中间加一个别的版本的模板就可以一起通过编译,灰常神奇啊~。~,这个匹配效果就是根据函数内申明然后匹配的~。~ 估摸着就是模板定义最后一个特例被类内特例模板申明对应~。~