Remix.run Logo
▲ adev_ 2 hours ago

What you wrote there is just an unsafe way to do std::variant. This has nothing to do with what the blog post is about.

The blog is about Open polymorphism. Where the list of compatible Object can be extended dynamically (Which is handy for some type of error report for instance).

You can do that since forever in C++ (Since the first support for inheritance), the only thing std::polymorphism brings is the ability to do it without dynamic allocations, emplacement new or lifetime problems.

▲nananana9 2 hours ago | parent [-]

> This has nothing to do with what the blog post is about.

It's an alternative way to solve the problem presented, which trivially allows you to make a std::vector<Event> (the whole point the article is building up to).

> the only thing std::polymorphism brings is the ability to do it without dynamic allocations, emplacement new or lifetime problems.

Which the version I showed has none of.

▲adev_ 2 hours ago | parent | next [-]

> it's an alternative way to solve the problem presented

No it's not and you apparently did not understand the concept behind it (or more likely, do not want to understand the concept behind it).

Your solution is not an implementation of Open Polymorphism, it is just a poor man implementation of std::variant.

> Which the version I showed has none of.

Because it is not the same thing like explain by my previous comment and in around 5 other comments around mine soon I guess.

But you are entirely free to grunt around and continue to imply that C++ committee people are implementing something useless (Which they are not, this is useful).

▲nananana9 2 hours ago | parent [-]

My point is that you don't need open polymorphism for the example given, and if you give me another example, I can show you why you don't need it there either -- because you do in fact never need it. It's an XY problem over the set X.

If a programmer is reaching for a library, they want it to solve a concrete task. Nobody wants to subclass your Event type, if I have to do it, you've failed at your job.

▲adev_ 42 minutes ago | parent [-]

> Nobody wants to subclass your Event type, if I have to do it, you've failed at your job.

Because it's well known that subclassing is used only for Event type and never EVER anything else requires polymorphism #irony.

The only part of this discussion where you are right is that closed polymorphism (std:: variant) is better than open polymorphism when it can be applied.

And even there, your proposed implementation is utter garbage that leaks memory and that I would refuse straight away in any PR.

▲OskarS 2 hours ago | parent | prev [-]

> Which the version I showed has none of.

No, it has something much worse, it has a memory leak. The nested types inside the union allocate memory, and they don't get destructed when Event gets destructed. Don't do this in modern C++, it's a terrible terrible idea, these kinds of constructs are very hard to get correct manually. Just use std::variant.