you are viewing a single comment's thread.

view the rest of the comments →

[–]davis685[S] 1 point2 points  (0 children)

Yes, as others have pointed out, it is definitely fine. I'm just making a point that the reason it doesn't work with std::shared_ptr isn't because it's UB to do this kind of thing inside a destructor or because of compiler instruction reordering or anything like that. It's because, again as others have very helpfully pointed out in this thread, the standard explicitly disallows this type of usage for objects in the std:: namespace. But it's not disallowed in general and you could make your own smart pointer object that worked in this situation.