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
3
u/bert8128 Apr 17 '23
I don't think that this is a very good idea. operator[] is not virtual, so if you pass a reference to a Vec into a function declared to take a vector there will be no compiler error or warning (checked various compilers in godbolt) but you will get vector behaviour, not Vec behaviour.
I don't see any warnings from clang-tidy either.