模板,只在头文件中编码? [英] Templates, coding only in header file?

查看:36
本文介绍了模板,只在头文件中编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我了解到,如果我想使用模板,我应该只在头文件中实现代码.我知道有解决方案可以在 .h 和 .cpp 文件中做到这一点,但只是假设我不想要要做到这一点,我应该在 .h 文件中编写所有代码吗?即使程序可能很大,这是一种编写代码的好方法吗?另外,只有 .h 文件(不包括 main 函数)并且从不使用 .cpp 文件看起来很奇怪.

Recently I've learned that if i want to use templates, I should implement code only in header files.I know that there are solutions to do that in .h and .cpp files, but just supposing i don't want to do that, should i write all my code in the .h file? Is it a good way of writing code, even though the program can be large? In addition, it looks weird to have only .h files(not including main function) and never use .cpp file.

推荐答案

将声明和接口文档与实现分开,就像您通常对 header (*.h) 和源文件 (*.cpp) 由于 C++ 编译过程,通常模板无法使用.

Separating the declaration and the interface documentation from the implementation like you generally do with header (*.h) and source files (*.cpp) is generally not possible with templates due to the C++ compilation process.

在编译过程中,所有头文件都包含在调用它们的源文件中,然后这些源文件被独立处理,最后所有生成的目标文件链接在一起成为一个可执行文件.模板是通用数据类型的函数,它们仅针对调用它们的数据类型进行实例化.这意味着模板函数不会创建任何代码,只要它没有被具有有效模板参数的人实例化.您必须以某种方式确保使用访问模板函数的所有源文件所需的数据类型实例化模板.

In the compilation process all the header files are just included into the source files they are called from, then these source files are processed independently and finally all the resulting object files linked together into an executable. Templates are functions for generic data-types which are only instantiated for the data-types they are called with. This means a template function does not create any code as long as it is not instantiated by somebody with valid template arguments. Somehow you have to make sure that the template is instantiated with the data types required from all the source files that access the template function.

  • 如果您将声明放在头文件中并将实现放在源文件中(就像您通常对函数所做的那样),则只会实例化在此特定源文件中已知的那些版本.这意味着需要不同参数集的任何其他源文件都可能导致链接器错误,因为它不知道源文件没有包含正确的模板参数组合,因此无法移植.
  • 您可以实例化所有必需的手动版本这会减少开销,但通常这会让您失去很多灵活性,因为您必须事先定义哪些模板参数将用于其他翻译单元.
  • 最后,您可以简单地将声明和实现留在头文件中.这会减慢编译过程,因为必须将标头的所有内容粘贴到调用它的所有源文件中.因此,有些人不喜欢高度模板化的库,例如 Boost,但它为您提供了最大的灵活性.这种影响虽然可以通过像 预编译头.为了使这更容易再次阅读,一些库将声明(和文档)与实现分开,将声明放在 *.h 文件中,并在 *.hpp 中定义实现>-files(类似于 * .cpp参见此处).
  • If you put the declaration in the header file and the implementation into the source file (like you generally do for functions) only those versions are instantiated that are know inside this particular source file. This means any other source file requiring a different set of parameters might result in a linker error as it is not aware that the source file does not hold the correct combination of template parameters making this not portable.
  • You could instantiate all the required versions manually which reduces overhead but generally this makes you lose a lot of flexibility as you have to define beforehand which template parameters will be used in other translatory units.
  • Finally you can simple leave the declaration and the implementation in the header file. This slows down the compilation process as all the contents of the header have to be pasted into all the source files calling it. Therefore some people are no fans of heavily templated libraries such as Boost but it gives you the most flexibility. This effect can though be reduced by features like pre-compiled headers. In order to make this more easily readable again some libraries separate declaration (and documentation) from the implementation by putting the declaration in *.h files and define the implementation in *.hpp-files (similar to * .cpp, see here).

所以通常这取决于用法:如果您知道特定模板函数仅在特定源文件中使用,您可以将文档、声明和实现放入源文件(而不是头文件)中.如果要与多个源文件共享模板函数,请将声明和实现放在标题中.这通常是标准方式,因为它最灵活.如果您编写了一个完全模板化的库,那么可能将 *.h 文件中的声明和文档与 *.hpp 文件中的实现分开可能是最有意义的,但主要只是为了有一个更简单的概述.无论如何,对于某些函数,声明和实现之间没有清晰的分离是完全正常的,因此某些头文件可能缺少相应的源文件.

So generally it depends on the usage: If you know that a particular template function is only used inside a particular source file you can put documentation, declaration and implementation into the source file (instead of the header). If you want to share the template function with several source files put the declaration and implementation inside the header. This is generally the standard way as it is most flexible. If you write a fully templated library then probably separating declaration and documentation in *.h files and implementation in *.hpp files probably makes the most sense but mainly just for having a simpler overview. Anyways it is completely normal to not have a clean separation between declaration and implementation for some functions and therefore some header files might be lacking a corresponding source file.

这篇关于模板,只在头文件中编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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