Unveiling Type Erasure in C++: From std::function to std::any

Type erasure is a powerful concept in C++ that allows us to work with heterogeneous types while hiding their concrete details behind a generic interface. In this talk, we will explore how the C++ Standard Library employs type erasure in constructs like std::function, std::shared_ptr, and std::any to provide flexible and efficient solutions for managing types at runtime. Further, we will unveil the hidden cost of using these constructs. We will start by building our own version of std::function, which will help demystify how it can store and call any callable object, regardless of its type. Then, we'll look at how std::shared_ptr uses type erasure to maintain type safety via dynamic dispatch of the deleter. Finally, we will delve into std::any, some times claimed the modern void*, and explore a different technique of type erasure.

Image

Sarthak Sehgal

Sarthak Sehgal is an early-career C++ Software Engineer at a high frequency options market making firm. His interests lie in low level programming, performance optimization, and finance. Blog: sartech.substack.com

Where

Toronto

When

July 20-23, 2025

LinkedIn

CppNorth Group