Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use a "tpp" file when implementing templated functions and classes defined in a header?

Tags:

c++

templates

Please refer to the first answer in this question about implementing templates.

Specifically, take note of this quote

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

I bolded the part that I'm most interested in.
What's the significance of a .tpp file? I tried doing exactly what was suggested in that page and it worked. But then, I changed the file extension to any random gibberish (like .zz or .ypp) and it still worked! Is it supposed to work? Does it matter if it's .tpp or any other extension? And why not use a .cpp?

Here's another thing I'm confused about.
If my implementations were written in a .cpp and the header defined non-templated functions, then I'd 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 defining templated functions and my implementations are in a file with a random funky extension, how is it compiled? And are the implementations compiled every time I compile whatever source code #includes said header?

like image 771
Manuel Avatar asked Jun 27 '17 06:06

Manuel


People also ask

What is a TPP file?

tpp file? It's used when you don't want the file that contains the interface of a module to contain all the gory implementation details. But you cannot write the implementation in a .cpp file because it's a template. So you do the best you can (not considering explicit instantiations and the like). For example.

Why are templates implemented in the header file?

To have all the information available, current compilers tend to require that a template must be fully defined whenever it is used. That includes all of its member functions and all template functions called from those. Consequently, template writers tend to place template definition in header files.

Why can't we compile template code into its own .O file?

Because the templates are not functions, they can't be compiled separately.

How are C++ templates implemented?

A class template is instantiated by passing a given set of types to it as template arguments. The C++ Standard Library contains many class templates, in particular the containers adapted from the Standard Template Library, such as vector .


1 Answers

Does it matter if it's .tpp or any other extension? And why not use a .cpp?

It does not matter what the extension is, but don't use .cpp because it goes against conventions (it will still work, but don't do it; .cpp files are generally source files). Other than that it's a matter of what your codebase uses. For example I (and the Boost codebase) use .ipp for this purpose.

What's the significance of a .tpp file?

It's used when you don't want the file that contains the interface of a module to contain all the gory implementation details. But you cannot write the implementation in a .cpp file because it's a template. So you do the best you can (not considering explicit instantiations and the like). For example

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 the implementations in a separate file is to save compilation time, so that you compile the implementations only once until you make some changes

You can't split up general template code into an implementation file. You need the full code visible in order to use the template, that's why you need to put everything in the header file. For more see Why can templates only be implemented in the header file?

But if the implementation file has some funky looking file extension, how does that work in terms of compiling? Is it as efficient as if the implementations were in a cpp?

You don't compile the .tpp, .ipp, -inl.h, etc files. They are just like header files, except that they are only included by other header files. You only compile source (.cpp, .cc) files.

like image 171
Curious Avatar answered Sep 20 '22 15:09

Curious