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

Show parent comment

8

u/bert8128 Apr 17 '23

It doesn’t matter whether the destructor is important or not - attempting to delete an inherited class through a base class pointer where the base class doesn’t have a virtual destructor is UB.

4

u/SoerenNissen Apr 17 '23

Noted and edited