Duck Typing, the C++ Way: How Type Erasure Bends the Rules - Sarthak Sehgal - CppCon 2025 @CppCon
Duck Typing, the C++ Way: How Type Erasure Bends the Rules - Sarthak Sehgal - CppCon 2025  @CppCon
Uploaded February 2026 | Updated November 2026, 17 hours ago
cppcon.org
---

Duck Typing, the C++ Way: How Type Erasure Bends the Rules - Sarthak Sehgal - CppCon 2025
---

Type erasure is one of C++'s most underrated yet powerful idioms - allowing you to write flexible, generic code while hiding concrete type information behind a unified interface. In this talk, we’ll dive deep into how type erasure enables runtime polymorphism without inheritance, and draw parallels to duck typing in dynamic languages, but with C++ static safety guarantees.

In this talk, we’ll explore how the C++ Standard Library leverages type erasure in tools like std::function and std::shared ptr, making it possible to store, dispatch, and manage heterogeneous objects with minimal friction. Along the way, we’ll uncover the inner workings of these abstractions by handcrafting a simplified version of std::function from scratch. We’ll also examine how std::shared ptr uses type erasure to maintain type safety while enabling polymorphic deletion. But it’s not all abstraction and elegance - we’ll peel back the layers to expose the hidden performance costs, memory allocations, and vtable machinery that come with these type-erased constructs. Through profiling and real-world scenarios, we’ll evaluate when type erasure is worth the trade-off, and when alternatives like templates or CRTP might be more appropriate.

By the end of the session, you’ll not only understand how type erasure works under the hood, but also gain the intuition to wield it judiciously - writing cleaner, more flexible, and performance-aware modern C++ code.

---

Slides: github.com/CppCon/CppCon2025/blob/main/Presentations/Duck_Typing_the_Cpp_Way.pdf

Work at Hudson River Trading (HRT): tinyurl.com/safxfctf
---

Sarthak Sehgal

Sarthak Sehgal is a C++ Software Engineer at a high-frequency options market-making firm based in Chicago. He is passionate about low-level programming, performance optimization, and their applications in financial systems. Outside of work, he shares insights on C++ and finance on his blog, sartech.substack.com
---


CppCon is the annual, week-long face-to-face gathering for the entire C++ community. The conference is organized by the C++ community for the community. You will enjoy inspirational talks and a friendly atmosphere designed to help attendees learn from each other, meet interesting people, and generally have a stimulating experience. Taking place this year in Aurora, Colorado, near the Denver airport, and including multiple diverse tracks, the conference will appeal to anyone from C++ novices to experts.
Annual CppCon Conference - cppcon.org
linkedin.com/company/cppcon
https://x.com/cppcon
facebook.com/CppConference
reddit.com/r/cppcon
https://mastodon.social/@CppCon
---

Videos Filmed & Edited by Bash Films: BashFilms.com
YouTube Channel Managed by Digital Medium Ltd: events.digital-medium.co.uk
---

#cpp #cplusplus #cppcon #cppprogramming #cplusplusprogramming #softwaredevelopment #softwareengineering #coding #code #computerscience #technology #technews #programming #programmer
Duck Typing, the C++ Way: How Type Erasure Bends the Rules - Sarthak Sehgal - CppCon 2025Persistence Squared: Persisting Persistent Data Structures - Juan Pedro Bolivar Puente - CppCon 2025Can C++ Data Oriented Design Be ONE MILLION Times Faster? - Andrew DrakefordLearn Why Experts Are Excited About C++ Reflection At Barry Revzins CppCon 2025 Talk!LLMs in the Trenches: Boosting C++ System Programming with AI - Ion Todirel - CppCon 2025Lightning Talk: Improving an HTML Parser with Swift 6 Pattern Matching  - Andrew KasterLightning Talk: Only Forward: Why Concurrency Works in C++ - Thomas Cassell - CppCon 2025The Wonderful World of Designing a USB Stack Using Modern C++ - Madeline Schneider - CppCon 2025Parallel Range Algorithms: The Evolution of Parallelism in C++ - Ruslan Arutyunyan - CppCon 2025Back To Basics: C++ Strings and Character Sequences - Nicolai Josuttis - CppCon 2025Implementing Your Own C++ Atomics - Ben Saks - CppCon 2025Is Your AI Model Lying to You? The Reproducibility Problem
CppCon |

Duck Typing, the C++ Way: How Type Erasure Bends the Rules - Sarthak Sehgal - CppCon 2025

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER