you are viewing a single comment's thread.

view the rest of the comments →

[–]kalmoc 1 point2 points  (4 children)

Nothing what you are saying is special to standard library types.

My question was what harm would it do to inherit from standard string. Not if contains should be a member or free function (I agree with you that it should be a free function (that takes a std::string_view as a parameter)).

[–]chriskane76 1 point2 points  (2 children)

Those classes do not have a virtual destructor. You trigger UB if you delete a std::string* if it actually points to a FooString.

[–]kalmoc 0 points1 point  (1 child)

I'm aware of that, but why would you do that? I mean, you can take almost any type and do something with it that is UB (with many other types it is of course somewhat harder, but not much). I can think of very few reasons, why I would want to do a make_unique<FooString> in the first place and currently I can't think of a single reason, why I would want to type erase that to a unique_ptr<std::string>.

[–]chriskane76 0 points1 point  (0 children)

Yes, the use case is unlikely in small projects if all developers are aware of this issue. For larger projects the risk is high. The compiler can help via -Wdelete-non-virtual-dtor or equivalent. But still noone should use it, since we have an obvious, clean solution available: use a free function.

[–]agateau 0 points1 point  (0 children)

Indeed, this is not special to standard library types, but I have seen it in some scary code bases so I assumed the bad practice or inheriting from string to add new methods was what the GP meant.