Show HN: Beautiful Type Erasure with C++26 Reflection

99 points - today at 12:40 PM


Try it on Compiler Explorer: https://godbolt.org/z/91dj5jeGW

Check out the source code: https://github.com/RyanJK5/rjk-duck

Source

Comments

YesBox today at 8:31 PM
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 today at 2:21 PM
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 today at 1:01 PM
Reflections, especially static ones, are horrible for debugging.
gmueckl today at 4:29 PM
An include with a HTTP URL is a scary abomination straight put of hell. Please tell me that this is a compiler explorer specialty (which would still be cursed, but in a cool way) and not a GCC feature (which would be an absolute nightmare).
usrnm today at 7:57 PM
> 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

Or you just used void* and went on with your life

schaefer today at 2:35 PM
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?

rob74 today at 12:54 PM
The things people describe as "beautiful" never cease to amaze me...

...but, as they say, beauty is in the eye of the beholder!

semiinfinitely today at 5:33 PM
this is the most disgusting programming language ever invented!
deleted today at 12:42 PM
jrw0ng today at 4:17 PM
[dead]
therealdkz today at 7:24 PM
[dead]
deleted today at 12:40 PM
briandilley today at 3:09 PM
are we still hand writing code?