C++Now 2018: Louis Dionne “Runtime Polymorphism: Back to the Basics”
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about type erasure and storing heterogeneous types by value, since it explains non-inheritance approaches to runtime polymorphism.
Recommended by 2 people across 2 different months, 2019 to 2021.
What it is
Louis Dionne's C++Now 2018 talk surveys the design space for runtime polymorphism in C++ beyond classic virtual inheritance. He introduces a policy-based approach, implemented in his Dyno library, that supports stack, heap, shared or reference storage and customizable dispatch that can beat vtables in some cases.
What people said
Recommends metal · 2019-04 · Stack Overflow · on “How do you abstract without messing up your code?”
@πάνταῥεῖ and alterigel are right -- you need to use (preferably smart) pointers, not values in your vector to do traditional compile-time polymorphism. (There are some relatively obscure alternatives, e.g., run-time polymorphism. See Sean Parent's talk [LINK] or Louis Dionne's talk [this video] on the subject; libraries to do this also include Facebook's Folly.Poly [LINK] . If you're new to C++ and OOP, I'd stick with traditional polymorphism, however.)
Recommends Qaz · 2020-05 · Stack Overflow · on “Is it possible to create a vector of concept ? std::vector<Concept auto>”
If you have the time to invest, this video [this video] goes in-depth on techniques to implement this sort of type erasure. The link is to a timestamp with something very similar to the code in your question on the slide, but the majority of the talk discusses his actual solution, with this last part being focused on possible future language changes to enable simpler-to-use libraries.
Cites Barry · 2021-04 · Stack Overflow · on “How to create a vector of objects that share a concept?”
What you're looking for is typically called "type erasure." The C++20 concept s language feature does not (and cannot) support type erasure - that feature is limited entirely to constraining templates (class templates, function templates, member functions of class templates, etc.) and cannot really be used in any other context. You'll have to instead either write your type erased Talk able by hand or resort to using one of the available type erasure libraries. For example, with dyno [LINK] (which Louis Dionne gave several talks on CppCon 2017 [LINK] , CppNow 2018 [this video] ), this would look as follows. You'll note that the only place I'm using the concept Talk is to constrain the default conc