- Some classic (GoF) design patterns are no longer needed in C++ - 3 Updates
- comp.lang.c++.moderated update - 1 Update
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Jan 12 09:54PM On Fri, 12 Jan 2018 19:42:24 +0000 > Some classic (GoF) design patterns are no longer needed in C++. > Lambdas wrapped by std::function, for example, can be used instead of > the "command" pattern. You were arguing against this some years ago when I suggested something similar (although I was slightly more nuanced - see below). You were in pretty much full-on OO/inheritance mode then as I recall. > It can be argued that these new techniques are still implementing the > idea of the old patterns just not their substance, so knowledge of > the old patterns is still useful information to have. That is the correct view in my opinion. std::function avoids the need to implement virtual dispatch using hand written virtual functions and inheritance. It implements type erasure for you by itself employing virtual dispatch with one layer of inheritance: the heart of std::function is a virtual function call which is invoked by operator(). std::function simplifies not only the command pattern, but also the observer, strategy and visitor patterns. > that may arise as the cost of gaining type erasure, penalties that > may be more significant than those present in the classic pattern > implementations. Penalties? Type erasure using std::function requires an allocation and a virtual function call. The command pattern implemented using abstract classes and inheritance for the same case requires an allocation and a virtual function call. Chris |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Jan 12 10:45PM On 12/01/2018 21:54, Chris Vine wrote: > You were arguing against this some years ago when I suggested > something similar (although I was slightly more nuanced - see below). > You were in pretty much full-on OO/inheritance mode then as I recall. I do not recall this at all. Maybe you are confusing me with someone else? > inheritance. It implements type erasure for you by itself employing > virtual dispatch with one layer of inheritance: the heart of > std::function is a virtual function call which is invoked by operator(). Yes I am well aware of how std::function works internally however others might not so it's good that you briefly explain it. > std::function simplifies not only the command pattern, but also the > observer, strategy and visitor patterns. Agree although sometimes I prefer an abstract interface when employing observer or similar (especially if there are multiple operations that can be logically grouped together). > a virtual function call. The command pattern implemented using abstract > classes and inheritance for the same case requires an allocation and a > virtual function call. Yes, penalties. I don't know for certain if this is still a problem with modern implementations but in the past I have seen that not all cases optimize to a single virtual function call; however, the biggest penalty is the allocation although often an SSO-esque technique will be employed to mitigate that for most cases. With the classic "command" pattern implementation it is possible to have direct control over allocation but since C++17 it has not been possible to customize std::function allocation (which was a mistake IMO). /Flibble -- "Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Bryne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?" "I'd say, bone cancer in children? What's that about?" Fry replied. "How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil." "Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say." |
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Jan 12 11:12PM On Fri, 12 Jan 2018 22:45:36 +0000 > > I recall. > I do not recall this at all. Maybe you are confusing me with someone > else? I don't think so. You were not impressed by the idea of replacing the polymorphic behaviour of virtual functions with inheritance by std::function in order to implement GoF abstractions, and described your OO credentials by reference to your role in bringing to market one of the first mobile phones, for Siemens if I recall it correctly. I think I described you as a 1990s OO dinosaur, or something like that. If you weren't involved in Siemens mobile phones then I have got the wrong person. > direct control over allocation but since C++17 it has not been > possible to customize std::function allocation (which was a mistake > IMO). You may be right but I remain to be convinced The essence of virtual dispatch in OO-style is that you do it through a pointer to a base class (usually abstract), which of necessity requires allocation on free store of the concrete object implementing the abstract interface. If your point is that you can use a fancy allocator for this (say, a specially designed small object allocator) when using the abstract class/inheritance approach but not when using std::function, then you may have a point. Dunno. Chris |
Ian Collins <ian-news@hotmail.com>: Jan 13 09:59AM +1300 On 01/12/2018 09:56 AM, Richard wrote: > on an email->news gateway that has been taken down. I'm working with > my ISP to get another email->news gateway established so that we can > resume moderation. Happy to help if to need coverage in my timezone. -- Ian. |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment