Beautiful Type Erasure with C++26 Reflection and rjk::duck
I built rjk::duck to solve the pain of complex type erasure by leveraging C++26 reflection. Instead of writing hundreds of lines of fragile code or relying on heavy libraries, you can now declare an interface once and let the compiler handle the rest. This single-header library uses new language features to generate vtables and resolve overloads automatically, offering high performance and full customization with minimal boilerplate.
We're talking about the bleeding edge here, so right now support is only available for gcc with -std=c++26 -freflection.
- YesBox
I use C++ every day and this feels like an entirely different language and philosophy. I know this is the reality of C++, but I never go searching for it so when it pops up randomly my jaw drops a little.
- Leherenn
What's compilation time like when using it?
I see there's an issue in the tracker to get more accurate data, and since it's using an under dev feature in compilers, it's not going to be definitive, but any rough numbers?
- feverzsj
Reflections, especially static ones, are horrible for debugging.
- rob74
The things people describe as "beautiful" never cease to amaze me...
...but, as they say, beauty is in the eye of the beholder!
- schaefer
in the first example:
```
10: rjk::duck<Container> c{std::vector<int>{1, 2, 3}};
11: c.size(); // 3
12:
13: c = std::string{"hello"};
```
Does the assignment on line 13 call the destrucor for the vector of ints created on line 10?