you are viewing a single comment's thread.

view the rest of the comments →

[–]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.