Due to the C++ core guidelines "Templates are the backbone of C++’s support for generic programming and class hierarchies the backbone of its support for object-oriented programming. The two language mechanisms can be used effectively in combination, but a few design pitfalls must be avoided." Let me see what this means.

Why is this due to the guidelines naively? This is in particular naively because the base class Container has many virtual functions. The presented designed introduces code bloat. Virtual functions are instantiated every time in a class template. In contrast, non-virtual functions are only instantiated if they are used.

Pimpl

Pimpl stands for "pointer to implementation" and means to remove implementation details of a class by placing them in a separate class, accessed through a pointer. This technique should be in the toolbox of each serious C++ programmer. Pimpl is often also called compilation firewall because this technique breaks the dependency between the implementation and the users of the class interface. This means the implementation can be changed without recompiling the user code.

Here is the general structure from Herb Sutters Blog: GotW #100: Compilation Firewalls.

The draw call of the class impl uses a back-reference to widget (lines (1) and (2)). The destructor and the move assignment operator (line (3)) must be user-defined in the implementation file base because std::unique_ptr requires that the pointed-to type is a complete type.

The program behaves as expected:

Besides its pros, the pimpl idiom has two cons: there is an additional pointer indirection, and you have to store the pointer.

What's next?

The next guidelines and, therefore, my next post is about variadic templates. Variadic templates are templates which can accept an arbitrary number of arguments. The rules in the guidelines to variadic templates as many rules to templates consist only of the headings. This means I write in my next post more general about variadic templates.

Get your e-book at Leanpub:

The C++ Standard Library

Concurrency With Modern C++

Get Both as one Bundle

With C++11,C++14, and C++17 we got a lot of new C++ libraries. In addition, the existing ones are greatly improved. The key idea of my book is to give you the necessary information to the current C++ libraries in about 200 pages.

C++11 is the first C++ standard that deals with concurrency. The story goes on with C++17 and will continue with C++20.

I'll give you a detailed insight in the current and the upcoming concurrency in C++. This insight includes the theory and a lot of practice with more the 100 source files.

Get my books "The C++ Standard Library" (including C++17) and "Concurrency with Modern C++" in a bundle.

In sum, you get more than 600 pages full of modern C++ and more than 100 source files presenting concurrency in practice.

Get your interactive course

Modern C++ Concurrency in Practice

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

Based on my book "Concurrency with Modern C++" educative.io created an interactive course.

What's Inside?

140 lessons

110 code playgrounds => Runs in the browser

78 code snippets

55 illustrations

Based on my book "The C++ Standard Library" educative.io created an interactive course.