关于C++ template 的初始化

新手可能会遇到编译没问题,自己写的模版类, 把声明(头文件.h)跟实现(cpp)分开, 编译时包括了模版的头文件, 编译没问题, 但连接不通过...是因为,如果你编译的代码模块中没用到那模版类的话, 它是不会帮你实例化的...而在另一个编译好的obj文件要用到模版类的时候, 会提示找不到那个符号..解决办法是需要你手动实例一个你将要用到类,  或者把模版的实现跟声明都在同一个文件
解决办法详见下文..

 Templates do not exist! (there is no spoon)

•Templates are a preprocessor construct.  They are cookie-cutters with which the preprocessor generates real C++ code.  When a template is used, (that is, specialized, implicitly or explicitly), it get instantiated.

•The instantiation tells the preprocessor to create a version of the template where each placeholder is replaced by its specialization.  At this point, the specific version of the template comes into existence and can be compiled.  It does not exist otherwise!

•In a very real way, a template just does a search and replace for each type you specialize the template for.  In essence, you are doing the same as writing a bunch of overloaded functions.  It’s just done for you, behind your back.

Here, the template has been implicitly specialized by its context.  It is within the specialization region of the class scope.  Thus it does not need the template arguments. For a class definition, the specialization region is the class block.


Notice that the specialization

region does not include the return type.  Thus the return type needs explicit specialization.

Remember that though constructors and destructors have the same name as  the class template, they are functions and do not need to be specialized.

Problem

Templates do not exist until you use them.  They must be instantiated.  Unless this is done explicitly, instantiation occurs at the first usage for all known template definitions.  Thus, consider this example.  Compile with

g++ -Wall –ansi main.cc Matrix.cc

   /* main.cc */
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值