Article

Beautiful Type Erasure with C++26 Reflection

lcapaldo ·Lobsters ·Published 2026-07-14

Building high-performance, modern C++ projects. If you’ve ever tried using type erasure for something more complicated than std::any or std::function , you’ve either written 100+ lines of easy-to-mess-up code or reached for a…

From the article

Building high-performance, modern C++ projects.

If you’ve ever tried using type erasure for something more complicated than std::any or std::function , you’ve either written 100+ lines of easy-to-mess-up code or reached for a boilerplate-heavy library like Boost.TypeErasure or Folly.Poly . rjk::duck uses the magic that is C++26 reflection to remove these pain points while preserving all of the customization and performance.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <rjk/duck.hpp> // ... struct [[ = rjk :: trait ]] Container { auto size () const -> std :: size_t ; auto empty () const -> bool ; auto clear () -> void ; }; rjk :: duck < Container > c { std :: vector < int > { 1 , 2 , 3 }}; c . size (); // 3 c = std :: string { "hello" }; // swap underlying type at runtime c .…


Share this resource


Discovered 2026-08-02 Source Lobsters Archive 2026-08 →