CppCon 2017: Louis Dionne “Runtime Polymorphism: Back to the Basics”
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about polymorphism with value semantics, contiguous storage of polymorphic objects, and type erasure as an alternative to inheritance hierarchies.
Recommended by 2 people across 2 different months, 2017 to 2021.
What it is
Louis Dionne's CppCon 2017 talk explores the design space for runtime polymorphism in C++ beyond classic inheritance and virtual tables. He presents a policy-based approach using his Dyno library, covering stack, heap, shared and reference-semantics storage and custom dispatch mechanisms that can beat vtables.
What people said
Recommends Claire Andrews · 2017-10 · Stack Overflow · on “Contiguous storage of polymorphic types”
There was a talk at CppCon 2017, " Runtime Polymorphism - Back to the Basics [LINK] ", that discussed doing something like what you are asking for. The slides [LINK] are on github and a video of the talk is available on youtube [this video] . The speaker's experimental library for achieving this, "dyno", is also on github [LINK] .
Recommends metal · 2017-11 · Stack Overflow · on “Create an object without a name in C++”
Yes. By comparison to Java, you need to decide if you want to create it on the stack or on the heap. The former can have value semantics (behaves like an int -- copies/moves like an int, no polymorphic behavior), while the latter will have reference semantics (refers to the same object instance, can behave polymorphically, copy by cloning). void ref( const X& x ) { x.do(); } // Reference semantics void ptr( const X* const x ) { x->do();} // Reference semantics void val( const X x ) { x.do(); } // Value semantics int main() { ref( X{} ); // Created on the stack ptr( new X{} ); // Created on the heap, but leaks! val( X{} ); // Created on the stack, moved rather than copied if available auto x
Cites BeeOnRope · 2017-11 · Stack Overflow · on “Contiguous storage of polymorphic types”
@ColinAndrews - the video wasn't in fact available, but it has since been uploaded [this video] . I updated your question to include a link.
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 [this video] , CppNow 2018 [LINK] ), this would look as follows. You'll note that the only place I'm using the concept Talk is to constrain the default conc