# Deriving Type Erasure

DevFeed: [Deriving Type Erasure](<https://devfeed.tech/articles/deriving-type-erasure-60973.md>)

Original publisher: [Read original article](<https://david.alvarezrosa.com/posts/deriving-type-erasure/>)

Author: David Álvarez Rosa

Published: 2026-03-10T09:46:00Z

Content type: tutorial

Language: en

Sources: [David Álvarez Rosa](<https://devfeed.tech/sources/david-alvarez-rosa.md>)

Topics: [union types](<https://devfeed.tech/topics/union-types.md>), [Polymorphism](<https://devfeed.tech/topics/polymorphism.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [cpp](<https://devfeed.tech/tags/cpp.md>), [inheritance](<https://devfeed.tech/tags/inheritance.md>), [polymorphism](<https://devfeed.tech/tags/polymorphism.md>), [subclass](<https://devfeed.tech/tags/subclass.md>), [types](<https://devfeed.tech/tags/types.md>)

## AI overview

The tutorial explains type erasure in C++ by comparing interface-based polymorphism with templates. It introduces wrappers that can provide a shared interface for concrete types that do not share a base class, deriving a minimal std::any implementation.

## Source excerpt

Ever looked at std::any and wondered what's going on behind the scenes? Beneath the intimidating interface is a classic technique called type erasure: concrete types hidden behind a small, uniform wrapper. Starting from familiar tools like virtual functions and templates, we'll build a minimal std::any. By the end, you'll have a clear understanding of how type erasure works under the hood. Polymorphism with interfaces § The typical way to achieve polymorphism is to define an interface consisting of pure-virtual methods you want to be able to call. Then, for each implementation that you want to use polymorphically, you create a subclass that inherits from the base class and implement those methods. As an example, let's implement shape classes that have an area() method. We start with an interface1 class class Shape { public: virtual ~Shape() = default; virtual auto area() const noexcept -> double = 0; }; And add a couple of concrete implementations for Square and Circle class Square : public Shape { int side_; public: explicit Square(int side) noexcept : side_{side} {} auto area() const noexcept -> double override { return side_ * side_; } }; class Circle : public Shape { int radius_; public: explicit Circle(int radius) noexcept : radius_{radius} {} auto area() const noexcept -> double override { return std::numbers::pi * radius_ * radius_; } }; Now, we can use these implementations generically, by coding against the interface auto printArea(const Shape& shape) -> void { std::println("Area is {:.2f}", shape.area()); } Simple enough, right? Polymorphism with templates § Inheritance is a good solution to problems that require polymorphism, but sometimes the concrete types you want to handle polymorphically cannot share a common base class.2 In that case, if the types provide the same interface, you can use a template to get polymorphism instead auto printArea(const auto& shape) -> void { std::println("Area is {:.2f}", shape.area()); } You can use this method with Squar