【c++ templates读书笔记】【5】模板实战

1、模板声明和模板定义如果不在同一个文件中,在另一个文件中使用模板时会出现链接错误。

例子:

//Myfirst.h
#ifndef MYFIRST_H
#define MYFIRST_H

#include<iostream>
#include<typeinfo>
using namespace std;

template<typename T>
void print_typeof(T const& x);

#endif

//Myfirst.cpp
#include"Myfirst.h"
#include<iostream>
#include<typeinfo>
using namespace std;

template<typename T>
void print_typof(T const& x){
	cout << typeid(x).name() << endl;
}

//main.cpp
#include"Myfirst.h"

int main(){
	double ice = 3.0;
	print_typeof(ice);

	system("pause");
	return 0;
}

原因:函数模板print_typeof()的定义还没有被实例化。当实例化一个模板时,编译器必须知道应该实例化哪个定义及要基于哪个模板实参进行实例化。编译器在MyFirst.h文件中看到了模板的声明,但没有模板的定义,这样编译器就不能创建voidprint_typof(double const& x),但这时并不出错,因为编译器认为模板定义在其它文件中,并产生一个指向该定义的引用,让链接器利用该引用解决这个问题。

2、解决以上问题可以有以下几种方法:

2.1、包含模型:把模板的定义包含在声明模板的头文件里

//Myfirst.h
#ifndef MYFIRST_H
#define MYFIRST_H

#include<iostream>
#include<typeinfo>
using namespace std;

template<typename T>
void print_typeof(T const& x);

template<typename T>
void print_typeof(T const& x){
	cout << typeid(x).name() << endl;
}

#endif
//main.cpp
#include"Myfirst.h"

int main(){
	double ice = 3.0;
	print_typeof(ice);

	system("pause");
	return 0;
}

包含模型的缺点:a、增加了头文件的开销 b、大大增加了编译复杂程序所耗费的时间

2.2、显示实例化

在1的基础上,添加MyFirstinst.cpp

//Myfirst.h
#ifndef MYFIRST_H
#define MYFIRST_H

template<typename T>
void print_typeof(T const& x);

#endif
//Myfirst.cpp
#include"Myfirst.h"
#include<iostream>
#include<typeinfo>
using namespace std;

template<typename T>
void print_typeof(T const& x){
	cout << typeid(x).name() << endl;
}
//Myfirstinst.cpp
#include"Myfirst.cpp"

template void print_typeof<double>(double const& x);
//main.cpp
#include"Myfirst.h"
#include<iostream>
using namespace std;

int main(){
	double ice = 3.0;
	print_typeof(ice);

	system("pause");
	return 0;
}

2.3、分离模型:在模板定义和声明前加上关键字export

如函数模板声明:

//Myfirst.h
#ifndef MYFIRST_H
#define MYFIRST_H

export template<typename T>
void print_typeof(T const& x);

#endif

但VS 2013编译器还不支持。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值