C++ Course: C++ Templates - The Proper Dose


In this course you will learn how to use templates in C++ up to C++20 to write more robust code.

Participants will learn

  • have a good understanding of the different template elements
  • understand variadic templates and their advantages
  • be able to constrain templates with, for example the help of type traits

Duration

3 days

Prerequisites

Developers of C++ based systems. A good knowledge of the C++.

Course Description

Almost all C++ programmers used templates, often indirectly by using the STL (Standard Template Library). Although templates were already part of C++ very early on, there are still reservations and misunderstandings. The course shows that templates can be a real benefit, especially in the area of embedded systems.

Used correctly, templates are a very helpful tool that C++ users should be familiar with.

Course Content

In this course, participants learn how to use and control C++ templates. Modern standards up to C++20 are used. Aspects such as resulting code size, compatibility, and readability are always taken into account.

Participants learn how templates can be used to detect errors at compile time and prevent unwanted conversions. In the further course, the use in the area of embedded systems is discussed, for example, to statically allocate memory with templates.

The variadic templates introduced with C++11 are explained. These are of particular interest for software development in the MISRA environment, as they can deterministically accept a variable number of arguments. It is also discussed how it is possible with constexpr not only to instantiate some code at compile time but also to evaluate it directly there and only have the result in the resulting binary.

After the training, participants will know how templates can be used safely and sensibly in practice. They are aware of the effects and control them.

Course Outline

  • Template Basics
    • What is generic programming
    • The different kinds of template parameters
    • Function templates
    • Class templates
    • Dependent names
    • Specializations
    • Primary templates
    • Partial specialization
    • Partial specialization: rules
    • Full specialization
    • Alias templates
    • Curiously Recurring Template Pattern
    • Placeholder data type as non-type template parameter
    • Type-Traits
    • Magic type-traits
    • Guidelines for efficient use of templates
    • Thinking in types
    • constexpr if
  • Advanced Templates
    • Testing at compile-time
    • Variadic templates
    • Fold-Expressions
    • Variable templates
    • SFINAE
    • Tag dispatch
    • Template template parameters
    • Policy design
    • extern template
    • typename vs. class
  • Template innovations in C++20
    • CTAD for aggregates
    • Concepts
    • Application areas for Concepts
    • Requires-expression
    • Simple requirement
    • Nested requirement
    • Compound requirement
    • Ad hoc constraints: requires requires
    • Defining a concept
    • Testing the created concept
    • Abbreviated function templates
    • Constrained placeholder variables
    • Floating-point numbers as NTTP
    • Classes as NTTP
    • Conditional explicit

Curious about how my C++ courses work?

Would you like to get a better idea of me and my teaching style in advance? Then I invite you to watch this excerpt from a recording of a one-hour online workshop.

Included in this course are

  • A color PDF handout, which includes all references and an acronyms list.
  • The source code for the exercises as well as sample solutions for all exercises as a ZIP-file.
  • Certificate of attendance.
  • A complimentary copy of one of my books.
Notebook C++: Tips and Tricks with Templates
Notebook C++: About move semantics
Programming with C++20

What you can expect from my training courses.

My motto is "Write unique code." I like to reduce the code to the necessary parts. Duplication can be done using templates or functions so that, in the end, each individual piece of code clearly contributes to the code base. Only the unique code reduces maintenance. It gives you more time to focus on your next customer needs.

In order to write unique code in practice, all training courses have a high practical component. Impulses and presentations complement practical exercises. In any case, my sessions are very interactive. I like to hear what the participants think to give the right tips. This creates a training experience in which each participant learns something new and achieves their learning success. The training components build on each other didactically and in terms of content. And I answer questions at any time.

The live demo parts are an essential part of my lessons. I use the following two tools there:

Request course now

Not the right course?

You have not found the right training course or would like a modification? I would be happy to create a training course for your needs. Simply contact me with your request: Contact now

Looking for a course in German? I offer all my classes in German as well.

For individuals

You're an individual and don't want to wait for an open-enrollment course? You can check out my self-study course which is a great alternative for you. Here is a fitting course.