Search⌘ K
AI Features

Idioms and Patterns: Type Erasure

Explore the concept of type erasure in C++ and how it enables handling various concrete types via a single generic interface. Understand different approaches including void pointers, object-orientation, and template-based implementations, and how this pattern supports flexible and reusable code design.

Type Erasure

Type Erasure enables you to use various concrete types through a single generic interface.

Type erasure is duck typing applied in C++

When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” (James Whitcomb Rileys)

Of course, you’ve already used type erasure in C++ or C. The C-ish way of type erasure is a void pointer; the C++ish way of type erasure is object-orientation.

Typical Use Case

Type erasure can be performed with void pointers, object-orientation, or templates. ...