4 ms·
Which problems from the article are you referring to? C++ is likely the worst OO language in common usage and C++ developers rightfully avoid using its native
by Kranar 6y ago
Which problems from the article are you referring to?
C++ is likely the worst OO language in common usage and C++ developers rightfully avoid using its native OO functionality as much as possible. They will literally jump through hoops and write tons of additional boilerplate code to avoid exposing the language's OO functionality (doing what C++ developers call type-erasure).
C++11 didn't improve OO in C++, it gave programmers many features to avoid having to use it altogether.
- AnimalMuppet 6y agoI have literally never seen someone program C++ that way. I mean, it's probably true that some people somewhere are doing that. But saying "C++ developers rightfully avoid using its OO functionality as much as possible" seems like a complete distortion of the actual situation.
- Kranar 6y agoThose some people would be the authors of the standard library who use type erasure for std::function, std::string_view, std::span, std::any, std::ranges, std::fmt, the new class of pmr memory allocators use type erasure, the upcoming coroutines are built on type erasure. Common C++ libraries like boost have an entire library dedicated to type erasure: https://www.boost.org/doc/libs/1_75_0/doc/html/boost_typeerasure.html https://www.boost.org/doc/libs/1_75_0/doc/html/boost_typeera... Facebook's Folly library also provides type erasure functionality: https://github.com/facebook/folly/blob/master/folly/docs/Poly.md https://github.com/facebook/folly/blob/master/folly/docs/Pol... Google's Abseil is full of type erasure: https://abseil.io/ https://abseil.io/ Here is Adobe's C++ library for type erasure: https://stlab.adobe.com/group__poly__related.html https://stlab.adobe.com/group__poly__related.html And here's a talk by Sean Parent about how Adobe uses type erasure to emulate runtime polymorphism without using C++'s native OO features: https://www.youtube.com/watch?v=QGcVXgEVMJg https://www.youtube.com/watch?v=QGcVXgEVMJg I suppose these are just some people somewhere...
- pjmlp 6y agoAnd all of them use some kind of OOP ideas, while trying to sell themselves as not being OOP. classes, polymorphism, some level of inheritance, delegation, composition, builders, method dispatch, ....
- Kranar 6y agoNone of them are selling themselves as not being OOP. What they are doing is implementing an ad-hoc version of OOP that forgoes using C++'s native OO feature-set. Native C++ OO clashes with modern C++, often creating a dialect that does not play well with resource management and common C++ idioms. The type erasure libraries provide many of the traditional benefits of OO but allow you to retain value semantics (which native C++ OO does not). My argument was never about OO as a general concept, it was strictly that C++'s native OO feature set is very poor and modern C++ allows developers to move away from that feature set.
- AnimalMuppet 6y agoHow does native C++ OO not allow you to retain value semantics? Doesn't the Rule of 3 exactly give you value semantics? And it's quite standard to do that with what you're calling "native" C++ OO.
- Kranar 6y agoThe rule of 3 doesn't apply to C++ native polymorphic types and your statement is so far out of left field I'm guessing you don't do much C++ development. To humor you... if I have a base class of type Animal and a derived class of type Cat, then making a copy of a Cat through a pointer or reference to an Animal results in object slicing which is not what you expect when you make a copy of a polymorphic object. Basically you only end up copying the Animal instead of the entire Cat. https://en.wikipedia.org/wiki/Object_slicing https://en.wikipedia.org/wiki/Object_slicing One of the motivating reasons to use type erasure is precisely to solve this issue, so that one can copy an Animal or assign Animals and treat an Animal as a value and copies work as expected. When one copies a std::function or any of the type erased classes provided by the standard library, it just works as expected, without needing to consider lifetime issues, object slicing, C++'s complex rules about inheritance vs. virtual inheritance, and a host of other problems that don't mix well with other parts of the language. The cost of type erasure is that it requires a lot more work on the part of the author. The libraries I linked to help alleviate most of the boilerplate, but as the examples demonstrate, there's still a lot of work that needs to be done to get it right.