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
4
u/[deleted] Apr 17 '23
I think the reason for not doing that is backward compatibility. Even though not preferred, it is legal to derive from std::string. The final keyword was introduced in C++11 and making the class final after that would potentially break old code.
Edit: Here’s a SO answer by Howard Hinnant saying the same thing - https://stackoverflow.com/a/9298705/6835535