logoalt Hacker News

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

99 pointsby RyanJK5today at 12:40 PM41 commentsview on HN

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

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


Comments

YesBoxtoday 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.

Leherenntoday 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?

show 1 reply
feverzsjtoday at 1:01 PM

Reflections, especially static ones, are horrible for debugging.

show 2 replies
usrnmtoday 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

show 1 reply
gmueckltoday 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).

show 1 reply
schaefertoday 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?

show 2 replies
rob74today 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!

show 3 replies
semiinfinitelytoday at 5:33 PM

this is the most disgusting programming language ever invented!

show 1 reply
RyanJK5today at 12:42 PM

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

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

show 2 replies
jrw0ngtoday at 4:17 PM

[dead]

therealdkztoday at 7:24 PM

[dead]

briandilleytoday at 3:09 PM

are we still hand writing code?

show 1 reply