Idioms for Polymorphism and Templates


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

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() ... ; 



Rainer D 6 P2 540x540Modernes C++ Mentoring

Be part of my mentoring programs:





Do you want to stay informed about my mentoring programs: Subscribe via E-Mail.


Templates extend C++ with many new idioms.


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" ( 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::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 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, Ann Shatoff, and Rob North.


Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, and Slavko Radman.



My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small


My special thanks to PVS-Studio PVC Logo


My special thanks to logo


My special thanks to Take Up code TakeUpCode 450 60



I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

Bookable (Online)


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++
  • C++20

Contact Me

Modernes C++,







Stay Informed about my Mentoring



English Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Course: The All-in-One Guide to C++20

Course: Master Software Design Patterns and Architecture in C++

Subscribe to the newsletter (+ pdf bundle)

All tags

Blog archive

Source Code


Today 2109

Yesterday 4371

Week 37916

Month 168041

All 12055807

Currently are 191 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments