Idioms for Polymorphism and Templates
This post is a cross-post from www.ModernesCpp.com .
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
Polymorphism is the property that different types support the same interface. In C++, we distinguish between dynamic polymorphism and static polymorphism.
Dynamic 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 Base, and Base has Derived as a template argument.
template <typename T>
class Base {
...
};
class Derived : public Base<Derived> {
...
};
Static polymorphism is based on CRTP.
Static Polymorphism
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 . 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.
template<typename ... Ts>
struct Overload : Ts ... {
using Ts::operator() ... ;
};
Modernes C++ Mentoring
Stay Informed: Subscribe.
Templates
Templates extend C++ with many new idioms.
Mixins
Mixins are a popular idea in the design of classes to mix in new code. You can implement mixins in C++ by using CRTP. A prominent example is the class std::enable_shared_from_this .
Expression Templates
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.?
Policy
A policy is a generic function or class whose behavior can be configured. Typically, there are default values for the policy parameters. std::vector and std::unordered_map exemplify this design idea in the Standard Template Library.
template<class T, class Allocator std::allocator<T>>
class vector;
template<class Key,
class T,
class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class allocator = std::allocator<std::pair<const Key, T>>
class unordered_map;
Traits
Traits are class templates that provide characteristics of a generic type. They can extract one or more characteristics of a class template.
Tag Dispatching
Tag dispatching is a way to simulate function overloading based on concepts, often based on traits.
Type Erasure
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.
What's Next?
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, Animus24, 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, and Ann Shatoff.
Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, Ralf Abramowitsch, John Nebel, Mipko, and Alicja Kaminska.
My special thanks to Embarcadero , PVS-Studio , and Tipi.build .?
Seminars
I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
Bookable (Online)
German
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
New
Contact Me