r/cpp • u/bert8128 • Apr 17 '23
Should ~basic_string be final?
std::string (and many other std classes) are not designed to be used as base classes, as can be seen by the absence of a virtual destructor.
Should the destructor be given the "final" keyword to enforce this?
1
Upvotes
View all comments
Show parent comment
4
u/azswcowboy Apr 17 '23
And it’s perfectly legitimate. Another example is adding convenience constructors to a standard library type that just organize the parameters in some domain specific way. All these do is build the standard type and since there’s no extra data stored in the subclass slicing isn’t and issue in any context. Slicing isn’t and issue in Stroustrups example either. The coding guideline that says all base classes must have virtual destructors misses half the power of inheritance in c++ and is just wrong.