Sean Parent: Value Semantics and Concepts-based Polymorphism
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about polymorphism without inheritance, heterogeneous containers with value semantics, and type erasure in C++.
Recommended by 5 people across 4 different months, 2012 to 2023.
What it is
Sean Parent's C++Now 2012 talk on value semantics and concepts-based (runtime) polymorphism. He shows how to build polymorphic behavior from value types and type erasure instead of inheritance hierarchies and pointers. The recording runs about 1h43m and has poor audio.
What people said
Recommends Peter R · 2013-05 · Stack Overflow · on “What is the right way to use polymorphism and inheritance in C++ to handle a basket of fruit?”
This is almost a text book case for the Visitor [LINK] pattern. In the sample below I'm not using the standard visitor pattern class or function names, rather I'm using names that are relevant to the domain in this case - i.e. FruitProcessor and Process , rather than Visitor and Apply . I'm not going to comment much further as I think the code is pretty self explanatory other than to mention that this design requires the FruitProcessor to be manually updated when adding more types of fruit. There are various ways to handle that including registering fruit processing handlers or even using an abstract fruit processing factory (see abstract factory [LINK] for more details). I would like to men
Recommends gnzlbg · 2013-06 · Stack Overflow · on “How do you declare an interface in C++?”
In C++11 you can easily avoid inheritance altogether: struct Interface { explicit Interface(SomeType& other) : foo([=](){ return other.my_foo(); }), bar([=](){ return other.my_bar(); }), /*...*/ {} explicit Interface(SomeOtherType& other) : foo([=](){ return other.some_foo(); }), bar([=](){ return other.some_bar(); }), /*...*/ {} // you can add more types here... // or use a generic constructor: template<class T> explicit Interface(T& other) : foo([=](){ return other.foo(); }), bar([=](){ return other.bar(); }), /*...*/ {} const std::function<void(std::string)> foo; const std::function<void(std::string)> bar; // ... }; In this case, an Interface has reference semantics, i.e. you have to make
Recommends Sean Cline · 2013-09 · Stack Overflow · on “Shared pointer to an immutable type has value semantics”
Unfortunately, like all of the talks at Going Native 2013 [LINK] , it was constrained by a very tight time schedule. Fortunately for us, though, Sean Parent gave a much more thorough talk last year at C++Now called Value Semantics and Concepts-based Polymorphism [this video] . It covers the same material and will probably answer your questions. I'll take a shot at explaining anyway.... Introduction There are two types of semantics that a type can have: Value semantics. Reference semantics. (Sometimes called pointer semantics.) It is possible to go on for many pages about how the two differ and when one is preferable to the other. Let's simply say that code using value types can be more easily rea
Recommends llonesmiz · 2013-09 · Stack Overflow · on “Ad hoc polymorphism and heterogeneous containers with value semantics”
The sound is really bad, but this talk [this video] from last year's C++Now could be considered as an extended version of the one from Going Native. Link to the slides [LINK] . Just thought that it could be interesting.
Recommends TartanLlama · 2014-09 · Stack Overflow · on “Storing different object as void* and casting from and to void*”
There's almost certainly a better way to do this than storing `void*`. If you are trying to have polymorphic containers based on _functionality_ rather than inheritance, look into _Concept-Based Polymorphism_. Check here [LINK] for a brief introduction and here [this video] for more in-depth discussion.
Cites Ralph Tandetzky · 2013-09 · Stack Overflow · on “Ad hoc polymorphism and heterogeneous containers with value semantics”
Different alternatives It is possible. There are several alternative approaches to your problem. Each one has different advantages and drawbacks (I will explain each one): Create an interface and have a template class which implements this interface for different types. It should support cloning. Use boost::variant and visitation. Blending static and dynamic polymorphism For the first alternative you need to create an interface like this: class UsableInterface { public: virtual ~UsableInterface() {} virtual void use() = 0; virtual std::unique_ptr<UsableInterface> clone() const = 0; }; Obviously, you don't want to implement this interface by hand everytime you have a new type having the use()
Cites Ali · 2013-10 · Stack Overflow · on “using non-smart pointers in modern C++”
Short Version: Is there any acceptable reason for using non-smart pointers in modern C++? Short answer: Definitely, if they only serve for observation, that is, they don't own the pointee. However, try to use references instead of pointers even in this case; use pointers only if you really need to make them optional (initialize with null_ptr then reassign later, for example). Long Version: we have a huge product that contains lot's of old C++ code and now we are trying to refactor it to the modern C++ era. [...] Long answer: As I am reading these lines this answer comes to mind: Advice on working with legacy code [LINK] I wish I could upvote this answer more than once. I would quote: "[...]
Cites Oliv · 2018-02 · Stack Overflow · on “Design pattern alternative for factories : classes with different constructors”
You are not fool to lump these two concepts and the factory pattern will work for you. It often involves switch statement. So if you are learning, also consider returning a unique_ptr from your factory. Notice also that it is possible to design a wrapper class that hold the unique_ptr returned by the factory and where the factory is actualy the constructors of this wrapper class. This is known as Parent's or value type polymorphism. It is a kind of implementation of the strategy design pattern. video Parent's polymorphism [this video]
Cites Amir Kirsh · 2023-01 · Stack Overflow · on “Storing dynamically created objects”
The question is related to heap allocated objects vs. stack objects. There are already a few good resources on that in Stackoverflow: Stack Memory vs Heap Memory [LINK] Stack vs Heap C++ [LINK] C++ stack vs heap allocation [LINK] Why should I use a pointer rather than the object itself? [LINK] (you may want to look at all the above, each has its own angle). But, your question is specifically about a container holding objects or pointers to objects. For this there are a few other resources: Should I store entire objects, or pointers to objects in containers? [LINK] Is it better to save object pointer in STL container rather than object itself? [LINK] Let me summarize shortly: Usually working
Critical Kerrek SB · 2012-11 · Stack Overflow · on “Templated classes, how to represent as a member variable of another class without making this class templated”
Do you have a link? I only found this youtube video [this video] , which is apparently 100 minutes of terrible audio and a guy with a really, really boring voice. (Sorry, nothing personal, but just unwatchable.) Maybe there's a written version? [Edit:] Is Boost.TypeErasure in Boost?