Include .cpp at the end of the template header file

I read an older book of data structures, and he said that when you are programming template classes, you should include .cpp at the end of the .h file.

As far as I know, you need to perform full functional implementations in your .h file for any member functions of the template class - this is due to the way the template compiler works.

The only functions I taught could be placed in the implementation file for the template class were the template specialization functions, i.e.: template<> Class<Type>::function_name() .

Why is it suggested in this book to include .cpp at the end of .h? is this just a way to split implementations into different files when compiling them with a header? and if so, wherever you put the real specializations - I assume that they could not go into the .cpp included in the header.

+6
source share
5 answers

Most likely, the author prefers to have a declaration and definition in different files, I can guess that this is due to the fact that it simplifies the transition between declarations and definitions.

But having the "cpp" extension is a bit confusing. Usually these files are called "ipp" for "Inline C ++".

+9
source

This is probably the old language, and I think your analysis of the separation of implementation and declaration is correct. Back when this particular book was written, the author probably thought of the cpp file as the file in which the definitions were defined, and h files as the files that contained the declarations. Including real explicit specializations in the previous file would naturally be fatal (or at least useless) for the linker due to repeated definitions. For the time being, I would not name the .cpp definition file.

+3
source

Firstly, there is no rule that you must have implementations of templates in your .h file (or .hpp or .hh ); in fact, for anything but the simplest templates, I would recommend against this. What you do should include an implementation, regardless of the file. What kind of author probably meant to implement the implementation in a .cpp file and include it. I would recommend finding a different name, however, since most people (and some IDEs) assume that you should compile all .cpp files. The general convention is that .cc and .hh usual extensions for .tcc sources and headers for the implementation template; in the Windows world (where .cpp is almost universal), I would recommend something like .tpp .

Note that the earliest template implementations required the implementation to be in .cpp . These implementations did not require (or allow) its inclusion, however; the compiler performed a .cpp (or .cc ) search matching the .hpp (or .hh ) file, in which a template class definition or function declaration appeared, and generated a dummy file source file that included it (and all that was needed).

+1
source

As far as I know, you have to execute full-featured implementations in your .h for any member functions of the template class - this is because of the way the template compiler works.

It may be assumed that you have full functional implementations in your .cpp file.

If full specializations are in the cpp file, they should not be included in the header file. Most likely, it will not compile if you do this. Since the compiler will see several definitions of the same function as the header file, it will usually be included in several source files.

0
source

If you want to use a template, the entire template definition must be visible in the TU that creates the template instance. That way, you can simply enter the full definitions in the header file.

This is only if you really want the bodies of the classes and the bodies of the members of the class to be separated, then you might be tempted to put these function definitions in a separate file. However, the above rule still applies, so you will need to include a separate file along with the header so that anyone can use the template.

This is a matter of taste. If the member bodies are short, you can also define them in a string. Also keep in mind that functions defined inside a class definition are implicitly declared inline , while you will need to specify this explicitly if you are writing bodies separately.

Partially specialized templates are still templates, so the same rules apply.

A fully specialized template template is just an ordinary class, so you are considering as such:

 // FooInt.hpp template <typename> class Foo; template <> class Foo<int> { // your class here }; 

One thing you might have in mind is "explicitly instantiating a template", a la template class Foo<int>; . This is something else; leave a comment if you are interested in using this.

0
source

Source: https://habr.com/ru/post/898205/


All Articles