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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.