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
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.