Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ template and header files [duplicate]

So, I heard that C++ templates shouldn't be separated into a header (.h) and source (.cpp) files.

For instance, a template like this:

template <class T> class J {       T something; }; 

Is this true? Why is it so?

If because of that I'm gonna have to put both declaration and implementation in the same file, should I put it in a .h file or a .cpp file?

like image 651
kamikaze_pilot Avatar asked Apr 10 '11 15:04

kamikaze_pilot


People also ask

What happens if we include a header file twice in C?

If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time. This construct is commonly known as a wrapper #ifndef.

Does using templates save memory?

Templates do not save executable space. The executable space will be taken up whether you use a template or not. If you code up the same as the template, but changing the data type each time, you will not be saving code space, it is equivalent.

Where are C header files stored?

These are the directories that gcc looks in by default for the specified header files ( given that the header files are included in chevrons <>); 1. /usr/local/include/ --used for 3rd party header files. 2. /usr/include/ -- used for system header files.


2 Answers

Headers.

It's because templates are instantiated at compile-time, not link-time, and different translation units (roughly equivalent to your .cpp files) only "know about" each other at link-time. Headers tend to be widely "known about" at compile-time because you #include them in any translation unit that needs them.

Read https://isocpp.org/wiki/faq/templates for more.

like image 94
Lightness Races in Orbit Avatar answered Oct 02 '22 18:10

Lightness Races in Orbit


The reason you can't put a templated class into a .cpp file is because in order to "compile" a .cpp file you need to know what the type that is being used in place of T. As it stands a templated class (like your class J) doesn't have enough information to compile. Thus it must be all in headers.

If you want the break up the implementation into another file for cleanliness, the best practice is to use an .hxx file. Like this: inside your header file, J.h, put:

#ifndef _J_H__ #define _J_H__  template <class T> class J{  // member definitions };  #include "j.hxx"  #endif // _J_H__ 

and then, in j.hxx you'll have

template <class T> J<T>::J() { // constructor implementation }  template <class T> J<T>::~J() { // destructor implementation }  template <class T> void J<T>::memberFunc() { // memberFunc implementation }  // etc. 

Finally in your .cpp file that uses the templated class, let's call it K.cpp you'll have:

#include "J.h" // note that this always automatically includes J.hxx     void f(void) {      J<double> jinstance;  // now the compiler knows what the exact type is. } 
like image 30
Chris A. Avatar answered Oct 02 '22 18:10

Chris A.