How it works...

One of the oldest and most widely used features of C++ is C++ templates. Like inheritance, C++ templates are not generally described as a form of type erasure, but they are. Type erasure is nothing more than the act of removing or, in this case, ignoring type information.

Unlike the C language, however, type erasure in C++ generally attempts to avoid removing type information in favor of working around a type's strict definition while retaining type safety. One way to accomplish this is through the use of C++ templates. To better explain this, let's start with a simple example of a C++ template:

template<typename T>T pow2(T t){    return t * t;}

In the preceding example, we have created a simple function that calculates the power ...

Get Advanced C++ Programming Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.