A
A
Arthur2020-06-08 13:28:28
C++ / C#
Arthur, 2020-06-08 13:28:28

Large template classes (implementing CRTP). All shove into one .h file?

He began to move away from dynamic polymorphism towards static. I decided to apply the CRTP idiom, which has the peculiarity that the base class must be templated. The problem is that in the current implementation this very base class is quite large - ~400 lines. To make it template, all you have to do is drag and drop it into the .h file. This is fine? The question is not so much about this idiom, but in general about the use of large class templates. I would like to know the best practice in this regard

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Shatunov, 2020-06-08
@Carver182

Today, with the help of templates, a lot of preliminary calculations are done at the compilation stage, so as not to do these calculations manually and not to carry them out at run time. Not always such templates can boast of code compactness.
First of all, templates serve to automate code generation. The code that does not always require generation will be precisely compact or simple. Any smart pointer or standard container will easily go beyond the designated 400 lines just with its body. And if we also take into account the command to support such a standard template, then there the bill will go to thousands of lines .
It's important to understand this. Not only templates can generate code, but also the preprocessor. The preprocessor works in 4 stagestranslation, where all new code is generated at the level of memory blocks with text.
Template instantiation occurs already at the 8th stage, where the code is already generated by smaller nodes of the abstract syntax tree. The volume of the code of the template itself in this case practically does not spoil anything. However, the instantiation of each template occurs separately in each translation module requiring instantiation.
Both of these nuances indicate that both the preprocessor and templates can significantly reduce compilation time. Only, in one case, the preprocessor allocates memory a lot and for a long time, and in the other case, compilation time can eat up the laboriousness of instantiating the template.
Therefore, when working with templates, it is important to keep in mind the idea: do not get carried away with algorithms based on template instantiation. This means, for example, not to get carried away with recursive template functions and linear search at the expense of instantiation. In this day and age of variadic templates, this is pretty easy to forget.
With a reasonable restriction on the complexity of instantiation, the fact of instantiation for each translation module can not even be looked at.
And yet, by the way, this means that it is better to generate code not with macros, but with templates, as far as possible.
Regarding the organization of the code .
Template code can also be organized into sets of files. For templates, friendliness, pre-declaration, and out-of-place definition are also allowed.
Template implementations can be split between headers (.h files) and inline implementations (.inl files).
At the same time, the rule of accessibility of the template from the place of its instantiation remains important. Those. the entire group of files with the implementation of the template must be somehow related to the header of the template declaration, which is further planned to be provided to the user of the template.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question