templateInstance

// templateInstance.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "templateInstance.h"
#include "funs.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// The one and only application object

CWinApp theApp;

using namespace std;

//

int _tmain(int argc, _TCHAR* argv[])
{
	int c= add(5,6);
	//int c= add<int>(5,6); 
	cout <<c<< endl;
	return 0;
}

/*
11
请按任意键继续. . .
*/
// funs.h: interface for the funs class.
//
//

#if !defined(AFX_FUNS_H__9BEE7454_4A0C_48CB_BADD_D172C95C6DF4__INCLUDED_)
#define AFX_FUNS_H__9BEE7454_4A0C_48CB_BADD_D172C95C6DF4__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

template<typename T> 
T add(T a, T b);

//template<typename T> 
//T add(T a, T b)
//{
//	return a+b;
//}

//template int add<int>(int a, int b); //这一句有什么好处吗?

//手工的显示实例化....
//这个大概的意思是 特殊化。
//就是说你可以
//template<int>
// int add(int a, int b)
//{
//	return a + b + 1;
//}
//让int类型的add的特殊,区别与其他。。。虽然在这里举的例子不太正常,但是模版里面很多这么使用的。
//可以瞧瞧模板的元编程

#endif // !defined(AFX_FUNS_H__9BEE7454_4A0C_48CB_BADD_D172C95C6DF4__INCLUDED_)

// funs.cpp: implementation of the funs class.
//
//

#include "stdafx.h"
#include "templateInstance.h"
#include "funs.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//
// Construction/Destruction
//

template<typename T> 
T add(T a, T b)
{
	return a+b;
}

template int add<int>(int a, int b); //这一句有什么好处吗?


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值