And The Winner is: Templates


The future of C++ speaks templates, and so does my blog Modernes C++. Based on the poll of my last post "Quo Vadis - Modernes C++", my next presumably 50 - 100 posts explain the details to templates. In this post, I want to give you an idea of the upcoming posts.


Let me first write a few words about the poll because its result surprised me in two ways.

The Poll

First, about 400 people voted in the poll. This is a number I would not have expected. Thanks for the many votes.

Second, I assumed that "C++23" will probably win the poll, followed by "Templates"  or "Extending and embedding Python with C/C++". My assumption was so wrong. I'm happy that I made the poll.

When I'm done with this "Templates",  I make the next poll. Now, let's tear the wheel to templates.


Rainer D 6 P2 540x540Modernes C++ Mentoring

Stay informed about my mentoring programs.



Subscribe via E-Mail.


First of all: What are templates? Templates (class templates or function templates) are families of classes or functions.

Before I present my plan, I want to encourage you to write me an e-mail if I forgot a template-related feature you are interested in. Here is my e-mail address: This email address is being protected from spambots. You need JavaScript enabled to view it..

These are the upcoming topics with a few additional words:


Of course, my tour starts with the basics.

  • Presenting the instantiation of function templates provides you with the first intuition about templates. This holds in particular if function overloading or explicit template arguments are given.
  • In contrast to function templates, only the C++17-compiler can deduce the template arguments for class templates. Meaning, before C++17, you have to specify the template arguments such as int in std::vector<int>.
  • Template parameters can be types, non-types, and templates. In particular, floating-point values can be used in C++20 as non-type template parameters.
  • The compiler can automatically deduce the template arguments of templates. It makes a big difference if the template parameter is a reference/pointer, a universal reference (&&), or a value. Templates can also have default template arguments.
  • Class templates allow partial and full template specialization, function template only full template specialization.


After the basics, I want to provide you with more details.

  • In general, template instantiation happens implicitly but can also be done explicitly.
  • A variadic template is a template that can have an arbitrary number of parameters. Hence, templates can take an arbitrary number of arguments of any value category.
  • Based on variadic templates in C++11, we got fold expressions in C++17. Fold expressions reduce parameter packs on binary operators.
  • Friendship of templates is special. A class or class template can grand general friendship to each instance of a class template or function template. The friendship can also be granted to specific instances of a class template or function template,  and also to a specific type.
  • Names can be dependent on template parameters. For dependent names, you have to help the compiler to decide if the name is a type, a non-type, or a template. If ambiguous, the compiler assumes that a name is a non-type.


There are many techniques and features related to templates and template instantiation.

  • When you use a function template with at least two template parameters, you cannot decide in general, what should be the return type of the function template. Here, the automatic return type comes to our rescue. 
  • Template metaprogramming is turing complete. This means all that is calculable can be calculated at compile time. Template metaprogramming is a pure functional language embedded into the imperative language C++. Libraries such as many of the boost libraries use heavily template metaprogramming.
  • The type traits library is applied template metaprogramming. Thanks to the type traits library, you can perform type checks, type comparisons, and type modifications at compile time.
  • constexpr is the convenients way to program at compile time. In contrast to programming in a functional style using template metaprogramming, you can program with constexpr in an imperative style.
  • constexpr if enables it to compile source code conditionally.


Templates open C++ to new ways to design software.

  • Beside dynamic polymorphism (object orientation), we have static polymorphism (templates).
  • CRTP (curiously recurring template pattern) is a mind-blowing but very powerful technique. Thanks to CRTP, we can move the virtuality from run time to compile time.
  • Expression templates create structures, which stand for calculations. They are only evaluated when needed and spare, therefore, time and memory.
  • There are many idioms and patterns related to templates. For example, we have policies and traits, tag dispatching, and type erasure.


  • Concepts are an extension to the templates feature provided by the C++ programming language. Concepts are named Boolean predicates on template parameters, evaluated at compile time. A concept may be associated with a template (class template, function template, or member function of a class template), in which case it serves as a constraint: it limits the set of arguments that are accepted as template parameters.(Wikipedia)

What's next?

In my next post, I introduce function templates using the online compiler C++ Insights.


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, 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


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++,



-1 #1 S Thomas 2021-05-02 21:08
typo: "Template metaprogramming is turning complete. "


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

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

Subscribe to the newsletter (+ pdf bundle)

All tags

Blog archive

Source Code


Today 437

Yesterday 7888

Week 8325

Month 152496

All 11633650

Currently are 162 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments