Due to the C++ core guidelines, “Templates are the backbone of C++’s support for generic programming and class hierarchies the backbone of its support for object-oriented programming. The two language mechanisms can be combined effectively, but a few design pitfalls must be avoided.” Let me see what this means.
This section consists of five rules.
- T.80: Do not naively templatize a class hierarchy
- T.81: Do not mix hierarchies and arrays
- T.82: Linearize a hierarchy when virtual functions are undesirable
- T.83: Do not declare a member function template virtual
- T.84: Use a non-template core implementation to provide an ABI-stable interface
Rule T.81 is too loosely related to templates and hierarchies, and rule T.82 is empty; therefore, my post boils down to the three remaining rules.
I will write about the rules T.80 and T.84 together because T.84 continues the story of T.80.
T.80: Do not naively templatize a class hierarchy, and T.84: Use a non-template core implementation to provide an ABI-stable interface
Here is an example a naively templatized class hierarchy from the guidelines:
Why is this due to the guidelines naively? This is, in particular, naively because the base class Container has many virtual functions. The presented design introduces code bloat. Virtual functions are instantiated every time in a class template. In contrast, non-virtual functions are only instantiated if they are used.
A simple test with CppInsight proves my point.
The program uses a std::vector<int> and a std::vector<std::string>.
CppInsight shows it. No method of std::vector is instantiated.
Here is the simplified program of the C++ core guidelines, including the virtual function sort.
Now, the virtual function sort is instantiated. Here is only the output of CppInsight, which shows the instantiation of the class template Container.
In total, I get 100 lines of code for the virtual function.
The note to the guidelines hints at how to overcome this code bloat. Often you can provide a stable interface by not parameterizing a base. This brings me to the related rule T.84: Use a non-template core implementation to provide an ABI-stable interface.
Pimpl stands for “pointer to implementation” and means to remove implementation details of a class by placing them in a separate class, accessed through a pointer. This technique should be in the toolbox of each serious C++ programmer. Pimpl is often also called a compilation firewall because this technique breaks the dependency between the implementation and the users of the class interface. This means the implementation can be changed without recompiling the user code.
Here is the general structure from Herb Sutters Blog: GotW #100: Compilation Firewalls.
These are the points of this idiom.
- put all private non-virtual members into impl (1)
- forward declare impl
- define impl in the corresponding implementation file (2)
Okay. Let me show a full example based on the one from cppreference.com.
The draw call of the class impl uses a back-reference to widget (lines (1) and (2)). The destructor and the move assignment operator (line (3)) must be user-defined in the implementation file base because std::unique_ptr requires that the pointed-to type is complete.
The program behaves as expected:
Besides its pros, the pimpl idiom has two cons: there is an additional pointer indirection, and you have to store the pointer.
The last guideline for this post is about a typical misconception.
Let me try to use a virtual member function template.
The error message from my GCC 8.2 compiler is crystal-clear:
The next guidelines and, therefore, my next post is about variadic templates. Variadic templates are templates that can accept an arbitrary number of arguments. The rules in the guidelines for variadic templates, as many rules to templates, consist only of the headings. This means I will write more generally about variadic templates in my next post.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
|My special thanks to SHAVEDYAKS|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,