(no title)
brtv | 2 years ago
This is pretty much impossible when holding a pointer of base class. However, this is a primary reason for having pointers in the first place (polymorphism, and having abstract base classes).
In all other cases, you're probably better off with either the raw value, std::variant or std::reference_wrapper.
gpderetta|2 years ago
For example shared-ptr to base can correctly invoke the correct derived type destructor even if the destructor is not virtual.
Edit: accidentally a word.
brtv|2 years ago
> For example shared-ptr to base can correctly invoke the correct derived type
Invoke what exactly? Im sorry I don't understand what you're trying to say here.
I guess you can force all derivied types to implement a clone() function, such that box<T> can do the deep copy, but Id consider that a fairly big inconvenience for such a simple pointer type.
liquidify|2 years ago
nly|2 years ago
I find it a delight
seeknotfind|2 years ago
brtv|2 years ago
I'm not a big fan of RTTI, and not even sure if it would work here. But once you start keeping track of all derived types, you might as well use an std:: variant. It's more cache friendly too, so more performant in many cases.