模板声明和影响的影响定义 [英] Implications of template declaration & definition

查看:115
本文介绍了模板声明和影响的影响定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我所理解的模板类和模板函数(大部分)必须在同一个头文件中声明和定义。有了这个说法:

From what I understand template classes and template functions (for the most part) must be declared and defined in the same header file. With that said:


  1. 有没有其他方法来实现模板文件的单独编译,而不是使用特定的编译器?

  1. Are there any other ways to achieve separate compilation of template files other than using particular compilers? If yes, what are those?

如果有的话,在同一文件中声明和定义有什么缺点?

What, if any, are the drawbacks of having the declaration and definition in the same file?

什么是模板声明的最佳实践,定义?

What is considered best-practice when it comes to template declaration & definition?


推荐答案

如何整理模板源代码

基本上,您有以下选项:

Basically, you have the following options:


  • 在实例化的时候,让模板定义对编译器可见。

  • 在单独的编译单元中实例化您需要的类型,以便链接器可以找到它。

  • 使用关键字导出(如果有)

这篇关于模板声明和影响的影响定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆