This post is unique because I have written about all the topics mentioned in this post already. Therefore, I only provide a few words about the topic and a link to the post mentioned.
I decided on this particular post for two reasons. First, all the idioms for polymorphism and templates in C++ are very important and often used. Second, I don’t write posts about topics I have already a few months ago written about.
Polymorphism is the property that different types support the same interface. In C++, we distinguish between dynamic polymorphism and static polymorphism.
Dynamic Polymorphism takes place at run time, based on object orientation, and enables us to separate between the interface and the implementation of a class hierarchy. To get late binding, dynamic dispatch, or dispatch at run time, you need virtuality and an indirection such as a pointer or a reference.
Curiously Recurring Template Pattern (CRTP)
Here is the crucial idea of CRTP: A class
Derived derives from a class template
Derived as a template argument.
Static polymorphism is based on CRTP.
Static polymorphism happens at compile time and has no runtime performance cost.
The Overload Pattern
Typically, you use the overload pattern for a
std::variant is a type-safe union. with one value from one of its types. std::visit allows you to apply a visitor to it. Exactly here comes the Overload Pattern in C++20 very handy in play.
- Smart Tricks with Parameter Packs and Fold Expressions
- Visiting a std::variant with the Overload Pattern
Templates extend C++ with many new idioms.
Expression templates are typically used in linear algebra and are “structures representing a computation at compile-time, which structures are evaluated only as needed to produce efficient code for the entire computation” (https://en.wikipedia.org/wiki/Expression_templates). In other words, expression templates are only evaluated when needed.
A policy is a generic function or class whose behavior can be configured. Typically, there are default values for the policy parameters.
std::unordered_map exemplify this design idea in the Standard Template Library.
Traits are class templates that provide characteristics of a generic type. They can extract one or more characteristics of a class template.
Tag dispatching is a way to simulate function overloading based on concepts, often based on traits.
- Software Design with Traits and Tag Dispatching
- A std::advance Implementation with C++98, C++17, and C++20
Type Erasure enables using various concrete types through a single generic interface. In C, you base it on void pointers; in C++, on object orientation or templates.
I’m happy to present in my next post a guest post from Alex Eisenhut. Alex will write about his passion: good software architecture.
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, and Marco Parri Empoli.
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|
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,