Erasing the Concrete -- K-ballo

Have you heard of "type erasure"?

Erasing the Concrete

by K-ballo

From the article:

Type erasure is any technique in which a single type can be used to represent a wide variety of types that share a common interface. In the C++ lands, the term type-erasure is strongly associated with the particular technique that uses templates in the interface and dynamic polymorphism in the implementation.

A union is the simplest form of type erasure.

  • It is bounded, and all participating types have to be mentioned at the point of declaration.

A void pointer is a low-level form of type erasure. Functionality is provided by pointers to functions that operate on void* after casting it back to the appropriate type.

  • It is unbounded, but type unsafe.

Virtual functions offer a type safe form of type erasure. The underlying void and function pointers are generated by the compiler.

  • It is unbounded, but intrusive.
  • Has reference semantics.

A template based form of type erasure provides a natural C++ interface. The implementation is built on top of dynamic polymorphism.

  • It is unbounded and unintrusive.
  • Has value semantics.

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.