---
title: "Deriving Type Erasure"
url: https://daily.dev/posts/deriving-type-erasure-sljlvo2o1
source_url: https://david.alvarezrosa.com/posts/deriving-type-erasure
type: article
source: "C++"
published: 2026-08-15T20:08:47.564Z
updated: 2026-08-15T20:23:27.756Z
tags: ["c++"]
reading_time: 7
upvotes: 2
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Deriving Type Erasure

**[C\+\+](https://daily.dev/sources/isocpp)** · 7 min read · 2 upvotes · 0 comments

## Summary

A step-by-step derivation of how std::any's type erasure works internally, starting from familiar interface-based polymorphism using virtual functions, then showing the limitations of template-based polymorphism, and finally combining wrappers and templates to build a minimal, working type-erased 'Any' class from scratch using C++ shape examples.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://david.alvarezrosa.com/posts/deriving-type-erasure>

## Questions this post answers

### What is type erasure in C++ and how does std::any implement it internally?

Type erasure is a technique that hides concrete types behind a uniform interface using a combination of virtual functions and templates. An inner abstract 'Concept' class defines the interface, and a templated 'Model' class inherits from Concept, wraps a concrete object, and forwards calls to it. The outer class holds a unique_ptr to Concept, letting callers store any type without knowing it, which is exactly how std::any and std::function work under the hood.

_Developers untangling std::any internals can dig deeper into type erasure patterns on daily.dev._

### What are the downsides of using templates instead of virtual functions for polymorphism in C++?

Template-based polymorphism has two main drawbacks: each instantiation produces a distinct type, so there's no common base type to store mixed types in one container like a vector, and every caller of a templated function must either specify the concrete type explicitly or become a template itself to forward the type, which spreads templates across a codebase and increases compile times and binary size.

_Weighing templates against virtual interfaces for a C++ design is easier with resources like this on daily.dev._

## Similar posts on daily.dev

- [C\+\+26: std::polymorphic](https://daily.dev/posts/c-26-std-polymorphic-zsy0pbvxy) · Sandor Dargo · 1 upvotes · 0 comments
- [Erasing Existentials](https://daily.dev/posts/erasing-existentials-wccogvwj2) · Lobsters · 0 upvotes · 0 comments
- [Devirtualization and Static Polymorphism](https://daily.dev/posts/devirtualization-and-static-polymorphism-shj1ph5cl) · C\+\+ · 0 upvotes · 0 comments

---

Tags: [#c++](https://daily.dev/tags/c++)

[View this post on daily.dev](https://daily.dev/posts/deriving-type-erasure-sljlvo2o1)
