0

While making a template linked list, rather than having the files list.h and list.cpp, we create list.h and list.cpp.h. The .cpp.h file contains the implementation for the linked list class. At the end of list.h, before the #ENDIF, we #include "list.cpp.h".

I understand that by making the .cpp file a header file we are able to avoid compiling it, but how does this work? We have needed to compile implementations that rely on using "template < class T >" in the past, so why do we not have to compile this implementation file?

Edit: My question was marked as a duplicate with a link to Why can templates only be implemented in the header file? , which I had already read. That question does not answer why the template implementation does not need to be compiled like a normal .cpp implementation file does. If we can avoid compiling implementations by making them header files, why do we not do that for every implementation? What are the downsides?

Does instantiating a template compile the code for each instance of that template? If my .cpp ever requires the use of a template in one of its functions, then why wouldn't I change it to a header file if it avoids this initial compilation?

I hope my question makes more sense.

Community
  • 1
  • 1
shtuken
  • 171
  • 1
  • 9
  • 1
    *"I understand that by making the .cpp file a header file we are able to avoid compiling it,"* => Header files get compiled, it's just that *by convention* when putting things in a `.h` file you only put things that are safe to be included multiple times in a project. If you put `int foo;` into a header file and then `#include` that file in multiple source files, you'll get an error about the multiple declarations of `foo` at linker time. Templates are things in the category that it's safe to include multiple times...like inline function definitions, and they go in headers as the link says. – HostileFork says dont trust SE May 15 '16 at 23:52
  • It's not clear to me what you are asking. Code in a `.h` file gets compiled when a translation unit that does `#include "file.h"` gets compiled, because `#include` is a simple text replacement. When you say "why do we not do that for every implementation?", what do you mean exactly. You *do* put every template implementation in the header file (if it needs to be visible to multiple units) – M.M May 16 '16 at 00:07

2 Answers2

3

Remember, a #include directive just pulls the contents of the header into the file that's currently being compiled. So the template does get compiled; it gets compiled in every translation unit that #includes the file where the template is defined. And when the template is used (later in that file) it gets instantiated.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
1

In what you are saying, you are including the templates as header file, since your .h file includes the .ccp.h file in the end.

So this is a convention on your project, to seperate the normal .h and the template code in two files.

Hence the template code, in your case, is compiled into the regular .cpp code.

The reason why you would want to avoid that is "bloat" -- every .cpp file will create it's own copy of the template code used in that cpp-unit, so if if A.cpp and B.cpp both includes T.cpp.h, they may both create the same code if they use the same templates, and it takes a smart optimizer to remove all the duplication.

Soren
  • 14,402
  • 4
  • 41
  • 67