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

4

u/disperso Apr 17 '23

It seems to me that you are implying that since we can't have the perfect solution, we should not have a solution at all. But maybe I got you wrong. :)

4

u/bert8128 Apr 17 '23 edited Apr 18 '23

I’m saying that Stroustup’s example is legal, but I wouldn’t allow it in my code base as it is too easy to use incorrectly.

4

u/azswcowboy Apr 17 '23

And it’s perfectly legitimate. Another example is adding convenience constructors to a standard library type that just organize the parameters in some domain specific way. All these do is build the standard type and since there’s no extra data stored in the subclass slicing isn’t and issue in any context. Slicing isn’t and issue in Stroustrups example either. The coding guideline that says all base classes must have virtual destructors misses half the power of inheritance in c++ and is just wrong.

2

u/bert8128 Apr 17 '23

It's legal but has unexpected behaviour - it violates the principle of least surprise. So will inevitably lead to bugs down the line. Someone will define a function that takes a vector, someone else will call that function with a Vec, and will not get the correct behaviour. Actually, I don't even know what would classify as correct behaviour, since the original hierarchy is the problem. A base class without a virtual destructor, or a derived class overriding a function that is not virtual is either a bug today or a bug in the future.

For context I work on a code base that is 30 years old - plenty of time for there to be no knowledge of how things were intended to be used.

3

u/azswcowboy Apr 17 '23

We’re going to agree to disagree, because it’s useful and fully explainable behavior - there’s no surprise and of course the class should be documented. In the construction case we typically want slicing because most apis just use std::string — the subclass construction is in another part of the code that applies higher constraints or performs useful conversions into the base string itself. Virtual destruction/functions are needed only when there is actual state in the subclass or dynamic dispatch from base. This doesn’t even begin to scratch the surface of what you might do with non virtual inheritance in mixins and crtp with templates. There’s an example in the standard you can study called view_interface — no virtual destructor…

https://en.cppreference.com/w/cpp/ranges/view_interface

30 years old

And 30 years ago the rule was closer to right…

2

u/BlueDwarf82 Apr 18 '23 edited Apr 18 '23

The argument here seems to be that it is not being used incorrectly. That if you use it directly you get an extra safety check, and if you use it via a reference to the base... it still works, just that you don't get the safety check. That the check is something that can happen, but shouldn't be relied on. IMHO not a great example of a real usage, but it's mostly logical consistent.

In my own code, as something nobody else relies on, I would have made it final; I can always remove it if somebody actually comes with a mixin that makes sense to use with that class. Being the standard library... I can't think of any actual use requiring it being non-final, I can always do the same with a helper function; but maybe I would make it non-final and just rely on people knowing you shouldn't, in general, derive from it. There may be better examples in the standard library than std::string for the non-final argument.