Why use the "tpp" file when implementing the template functions and classes defined in the header?

Pay attention to the first answer to this question on the implementation of templates.

In particular, pay attention to this quote.

A common solution for this is to write the template declaration in the header file, and then implement the class in the implementation file ( e.g. .tpp ) and include this implementation file at the end of the header.

I highlighted the part that interests me the most. What is the meaning of the .tpp file? I tried to do exactly what was suggested on this page, and it worked. But then I changed the file extension to any random nonsense (like .zz or .ypp) and it still works! Should this work? Does it matter .tpp or any other extension? And why not use .cpp?

Here's something else that I'm confused about. If my implementations were written in .cpp, and the header contains uncomplicated functions, then I only need to compile the .cpp file once, right? at least until I changed something in the .cpp file.

But if I have a header that defines template functions, and my implementations are in a file with a random funk extension, how is it compiled? And are there implementations compiled every time I compile any source code #include specified header?

+5
source share
3 answers

Does it matter .tpp or any other extension? And why not use .cpp?

It doesn’t matter what the extension is, but do not use .cpp because it is against the conventions (it will still work, but not do it; .cpp files are usually the source files). Also, this is a question of what your code base is using. For example, for this purpose I (and the Boost code base) use .ipp .

What is the meaning of the .tpp file?

Used when you do not want the file containing the module interface to contain all the details of the gory implementation. But you cannot write an implementation in a .cpp file because it is a template. This way you do your best (without considering explicit instances, etc.). for instance

Something.hpp

 #pragma once namespace space { template <typename Type> class Something { public: void some_interface(); }; } // namespace space #include "Something.ipp" 

Something.ipp

 #pragma once namespace space { template <typename Type> void Something<Type>::some_interface() { // the implementation } } // namespace space 

I thought the whole point of writing definitions in headers and implementations in a separate file is to save compilation time, so you only compile implementations once until you make some changes.

You cannot split a common code template into an implementation file. To use the template, you need the full code, so you need to put everything in the header file. For more details, see Why templates can only be implemented in the header file?

But if the implementation file has some kind of funky-looking file extension, how does it work in terms of compilation? How effective is it if the implementations were in cpp?

You will not compile .tpp , .ipp , -inl.h , etc. They are similar to header files, except that they are included only in other header files. You only compile source files ( .cpp , .cc ).

+9
source

File extensions do not make sense to the preprocessor; there is nothing holy in .h . This is just a convention, so other programmers know and understand what the file contains.

The preprocessor will allow you to include any file in any translation unit (this is a very dumb tool). Extensions like this just help clarify what should be included there.

+5
source

Does it matter .tpp or any other extension? And why not use .cpp?

It doesn't matter which extension is actually used, as it differs from any standard extension used for C ++ translation units.

The reason is the presence of a different file extension, as they are usually detected by any C ++ build systems for translation units ( .cpp , .cc , ...). Because translating them as a source file will fail. They must be #include d corresponding header file containing template declarations.

But if the implementation file has some kind of funky-looking file extension, how does it work in terms of compilation?

It must be #include d compiled as indicated.

How effective is it if the implementations were in cpp?

Well, not 100% as efficient relative to compilation time, as a pure object file created from a translation unit. It will be compiled again as soon as the header containing the #include statement changes.

And are the implementations compiled every time you compile any #include source code for the specified header?

Yes they are.

+3
source

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


All Articles