More C++ Idioms/Polymorphic Value Types

= Polymorphic Value Types =

Intent
Support run-time polymorphism of value types without requiring inheritance relationship.

Also Known As
Run-time Concept

Motivation
Sometimes, one wants a type-erased container for types that implement some interface.

Solution and Sample Code
By defining a wrapper class template that accepts types satisfying an interface and implements that interface itself by forwarding, but inherits from a base class that requires that derived classes implement that interface, then wrapping the wrapper class template and base class in another class, that class can exhibit polymorphic behavior without the stored values having an is-a relationship with an interface. The below class is a type-erased wrapper that stores any type that implements copying, equality comparison, and hashing. Its introduction into a codebase does not require any changes to types if they already implement the interface.

Known Uses

 * Adobe Poly Library
 * Adobe Poly Library
 * Adobe Poly Library

Related Idioms

 * Type Erasure