2 ms·
Those 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::
by Kranar 6y ago
Those 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.
- AnimalMuppet 6y ago> The 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. First: I've done professional C++ development since 1996. Fulltime, with just a smidgen of Java in a couple of places. So your guess is completely mistaken, as is your opinion of where I'm coming from. Second: The post I replied to never mentioned "polymorphic", so I interpreted "value" as your claiming that you couldn't treat a pre-C++11 class as a value type, which is clearly a bogus claim. I do mostly embedded systems. I do use polymorphism, but most of my objects cannot be cloned (private, unimplmented copy constructor and assignment operator), and the only collection they live in is a fixed-sized array. If I had to do your Cat example, I'd probably have Animal's copy constructor call a protected pure virtual clone() method. I understand the slicing problem, and that a slice is not what you want. I confess that I don't understand how type erasure helps with the copying problem, nor how std::function uses type erasure.