Better Code: Runtime Polymorphism - Sean Parent

NDC Conferences · 79K views · 2017 · 57:33

Open on YouTube

C, C++, Rust, Go & systems languagesSoftware architecture & designtalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering C++ questions about polymorphism, value semantics and type erasure, as the standard explanation of how to avoid inheritance hierarchies and pointer-based designs.

Recommended by 13 people across 13 different months, 2017 to 2025.

What it is

A 2017 NDC London conference talk by Sean Parent (Adobe) on implementing runtime polymorphism in C++ without inheritance, using value semantics and type erasure. It builds up a Photoshop-style document with an undo system using C++11 features.

What people said

Strong rec metal · 2017-09 · Stack Overflow · on “Calling subclass (virtual) function (late binding), when not dealing with pointers”

You can implement your classes using value semantics. Then you don't need pointers or references. See " Better Code: Runtime Polymorphism [this video] " by Sean Parent, which may just blow your mind. Here's the code [LINK] . Watch the talk and see how he uses value semantics to avoid bad data sharing and implement an impressive, polymorphic undo system. Here's an annotated sample: // A generic draw function that any type that streams to std::ostream can use template <typename T> void draw(const T& x, ostream& out, size_t position) { out << string(position, ' ') << x << endl; } // A class that can hold anything with value semantics // -- note: no virtual functions and no inheritance! class object_

Strong rec Maxim Egorushkin · 2018-03 · Stack Overflow · on “How to improve "=" operator overload for matrices?”

The ideal improvement would be Matrix& Matrix::operator=(const Matrix&) = default; . If you switch to using std::vector for matrix storage you won't need to implement the copy/move constructors/assignments and destructor at all. If what you are doing is a programming exercise, create your own dynamic array and use that in the implementation of your matrix. I cannot recommend enough watching Better Code: Runtime Polymorphism by Sean Parent [this video] , he makes an effective demonstration of why you should strive to write classes that do not require non-default implementations of copy/move constructors/assignments and destructor. Example: template<class T> class Matrix { std::vector<T> storage_;

Recommends AndyG · 2017-06 · Stack Overflow · on “C++ how can I define classes at runtime?”

It is impossible to actually create new classes, but your question did remind me of an interesting talk by Sean Parent, Runtime Polymorphism [this video]

Recommends AMA · 2017-10 · Stack Overflow · on “Storing a raw pointer returned from a method into a smart pointer”

Is the above a correct technique ? Yes, given that the LibraryMethod allocates the resource with new , and the library user is responsible for deleting it. Wrapping raw pointers with smart pointers is widely-used for legacy libraries. Am I causing a leak here ? No, if you do it correctly. I agree with other answers, but you can also do: std::shared_ptr<SomeClass> some_class(LibraryMethod()); Are there any better techniques to handle such a scenario ? One thing to carefully consider is the ownership of the resource allocated by LibraryMethod . Some argue that shared_ptr is evil in the sense that it is just a nicer way of introducing a global with dependencies which are hard to track. See this

Recommends Louis Langholtz · 2017-12 · Stack Overflow · on “Is there an equivalent of the Java <? extends ClassName> in C++?”

Equivalence may be a matter of exactly how we define equivalence (if not being opinion). If you want a runtime open container for any kind of animal... Sounds like a good opportunity for runtime polymorphism to solve. A runtime polymorphic solution seems functionally similar if not equivalent (at least in my thinking) to what you're asking for. The best description I know of for the runtime polymorphism that I mean is Sean Parent's "Better Code: Runtime Polymorphism" [this video] talk. Basically though the idea will hide the Animal inheritance hierarchy so from the container's perspective you can just use any container like using AnimalFarm = std::vector<Animal> . The details then go into the imp

Recommends Blasco · 2018-01 · Stack Overflow · on “How to handle Observables with different state-value types in the Observer”

I would recommend you to watch this talk on runtime polymorphism [this video] . I think it would inspire you to come with an elegant solution.

Recommends fadedreamz · 2019-10 · Stack Overflow · on “If base class pointer cannot access derived class member function, then what is so convenient about polymorphism?”

Run-time Polymorphism means polymorphic behavior at run time. The behavior defined by getArea() is changed at run time based on the instance it points to. So, you are mixing with polymorphism with static compiling problem. Base class does not have a member function name getNumberOfEdge() and you get compiler error. If you want to have specific implementation called, why not make compiler see it and cast it? public: virtual void getArea()=0; template<typename T> const T * get() { return dynamic_cast<T *>(this); } }; Now you can cast it to whatever derived type: auto square_instance = arr[0]->get<square>(); if (square_instance) { square_instance->getNumberOfEdge(); } A different approach to ru

Recommends Joel Filho · 2020-05 · Stack Overflow · on “C++ Array of Derived Classes”

CRTP is used for static polymorphism. If you need runtime polymorphism, there are many ways of achieving that. Virtual member functions with classic OOP inheritance is only one way. From the excellent " Embrace no Paradigm Programming! [LINK] ", we can see other ways we can do it and how they compare to each other: [LINK] If you're only using a known number of derived classes, I'd recommend using a variant -like data structure. It has static storage, so no unnecessary heap allocations, and all of the storage is abstracted from you. If you need to export the func() implementation explicitly, then you'd need to use polymorphic values ("type erasure" on the slides). See the classic Sean Parent

Recommends aep · 2020-07 · Stack Overflow · on “Heterogenous collection of CRTP-based polymorphic elements”

You can use `runtime-concept` idiom, which doesn't need run-time polymorphism at the interface level, but still you need to have runtime polymorphism at implementation level. [this video] link will provide you good information about the mechanism by Sean Parent.

Recommends facetus · 2020-12 · Stack Overflow · on “lvalue and rvalue getter, is it possible to remove them?”

The solution is to use std::shared_ptr<const std::string> . A shared pointer to a immutable object has value semantic. Copy-on-write can be achieved using shared_ptr::unique() . See Sean Parent presentation 47:46 [this video] .