0

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.

77jt777
  • 69
  • 1
  • 8

1 Answers1

0

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.

  • 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).

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.

2b-t
  • 2,414
  • 1
  • 10
  • 18