模板源代码组织

本文详细介绍了C++模板源代码的两种组织方式:包含模型和显式实例化,以及如何解决链接器错误。在包含模型中,模板声明和定义放在同一头文件;显式实例化则允许在特定编译单元实例化模板。通过理解这两种方法,开发者可以更好地管理和组织模板代码,避免编译和链接问题。
摘要由CSDN通过智能技术生成

源代码组织

非模板源代码组织:声明放在头文件中,具体定义放在源文件中。
对于非模板代码,一切正常运作,所有符号在整个程序中都是可见的。

但是,如果模板代码用这种方式组织,会出现链接器错误:

// MyTest.h
template <typename T>
void MyPrint(T const&);

// MyTest.cpp
#include "MyTest.h"

template <typename T>
void MyPrint(T const& X)
{
	cout << x << endl;
}

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

int main()
{
	MyPrint(5);
}

对于上面的代码,编译器可以顺利通过编译,但是链接器会提示找不到函数MyPrint()的定义。这个错误的原因在于:

  1. 编译器是按 编译单元 编译的,每个 编译单元 单独编译。
  2. 编译main.cpp时,编译器看到MyPrint()的调用,但是没找到MyPrint()的定义,编译器假设MyPrint()的定义在其它 编译单元,产生一个指向定义的引用,让链接器利用这个引用来解决问题。
  3. 我们知道,模板函数只有在被调用时才会被实例化,而每个 编译单元 单独编译。编译MyTest.cpp时,MyTest.cpp中没有调用MyPrint(),编译器也并不知道main.cpp中调用了MyPrint(5),所以MyTest.cpp中没有实例化MyPrint。

根据上面分析,不难猜测到,只要在MyTest.cpp中增加调用MyPrint()的代码,就可以顺利通过链接:

// MyTest.cpp
#include "MyTest.h"

template <typename T>
void MyPrint(T const& X)
{
	cout << x << endl;
}

// 增加下面代码后,顺利通过链接
void Test()
{
	MyPrint(5);
}

包含模型

对于模板源代码的链接问题,我们通常解决方法是包含模型:让模板的声明和定义位于同一个头文件中:

// MyTest.h
template <typename T>
void MyPrint(T const& X)
{
	cout << x << endl;
}

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

int main()
{
	MyPrint(5);
}

编译器编译main.cpp时,#include "Mytest.h"相当于把函数模板MyPrint的定义拷贝到了main.cpp中,当然就能顺利通过编译链接。
包含模型组织的代码清晰直观,但其缺点是增加了包含Mytest.h头文件的开销。主要的开销不在于模板函数实现代码本身,而在于为了支持其实现代码所包含的其它头文件。

显式实例化

解决模板源代码的链接问题的第二个方法是显式实例化:

// MyTest.h
template <typename T>
void MyPrint(T const&);

// MyTest.cpp
#include "MyTest.h"

template <typename T>
void MyPrint(T const& X)
{
	cout << x << endl;
}

// 用int显式实例化MyPrint()
template void MyPrint<int>(int const& x);

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

int main()
{
	MyPrint(5);
}

也可以给类模板的成员函数和类模板显式实例化,显式实例化类模板是,其所有类函数都会被实例化。

// 使用int显式实例化Stack<>
template class Stack<int>;

// 使用int显式实例化类模板成员函数
template void Stack<int>::push(int const&);

整合包含模型和显式实例化

可以自由选择包含模型和显式实例化的一种代码组织方法:把模板声明和定义放在两个头文件中。

//MyTest.h
template <typename T>
void MyPrint(T const&);

//MyTestDef.h
#include "MyTest.h"

template <typename T>
void MyPrint(T const& X)
{
	cout << x << endl;
}

如果想用包含模型,只需要#include头文件MyTest.h就可以:

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

int main()
{
	MyPrint(5);
}

如果想用显式实例化,需要#include头文件MyTest.h,并另外增加一个显式实例化的cpp文件:

//MyTest_Init.cpp
#include "MyTest.h"

template void MyPrint<int>(int const&);

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

int main()
{
	MyPrint(5);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值