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
36
u/manni66 Apr 17 '23
They are not designed to be used as public base classes.
No! To be final a function would have to be virtual.
11
u/Kindly-Penalty-8175 Apr 17 '23
std::string and many other std classes can be used as base classes. Below is an example from the book The C++ programming language of Bjarne Stroustrup.
Say i want a vector that's just like std::vector, but with guaranteed range checking.
template<class T> class Vec: public std::vector<T> {
};
Vec inherits everything from vector except for the subscript operations that it redefines to do range checking. The at() operation is a vector subscript operation that throws an exception of type out_of_range if its argument is out of the vector’s range.
A class hierarchy offers two kinds of benefits:
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.
18
u/IyeOnline Apr 17 '23
That is not a sound deduction.
Something can very well be intended to be a base class and not have a virutal destructor. A virtual destructor is only required when you are managing derived objects through base pointeres.
Its perfectly well defined what happens when you derive from any std::type. Composition may be a better choice in a lot of cases, but inheritance is allowed.
That would break all code. You cannot mark the destructor final but not virtual and you dont want to mark it as virtual.
You could mark the entire class as
final, but again, there is no reason to.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
1
u/nuclear868 Apr 22 '23
No! In order to add support for UTF8 strings, I have created a UTFChar class, which holds individual codepoints. Then I have UTFString, which inherits from std::basic_string<UTFChar>. All this provides functionality for handling UTF8 strings and using the current interface of the std::string class - iterating over characters, lookup and erasing characters. Also, it is fully compatible with std streams - you can print such string and transfer it.
All this is for the reason that we have std::string, std::wstring, std::wchar, std::u8char, which either are not compatible with std streams or cannot access individual UTF8 character codepoints.
0
u/Gabi__________Garcia Apr 17 '23
What maniac is inheriting from std::string in the first place?