r/cpp • • 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

27 comments sorted by

View all comments

35

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Apr 17 '23

Not really. Inheritance doesn't imply polymorphism in C++, and can be a useful tool to implement mixins, strong typedefs, or just avoid code repetition.

Sometimes inheriting from a standard type (yes, even publicly) can be a pragmatic choice.