再看模板特例定义问题……

// 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上测试例子的,我得到了几点:
首先模板主函数无特例的的时候(少红色部分),编译可以通过,模板是按照顺序显示的,第一个显示的是先出现的特例模板之一,
如果加上红色部分,那么显示就是根据类内函数顺序来的,特例被最后那个匹配~。~ ,其实两个特例的函数完全是相同的,中间加一个别的版本的模板就可以一起通过编译,灰常神奇啊~。~,这个匹配效果就是根据函数内申明然后匹配的~。~  估摸着就是模板定义最后一个特例被类内特例模板申明对应~。~
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值