4 ms·
Don't private destructors in c++ enable this?
by IcyWindows 2y ago
Don't private destructors in c++ enable this?
- jandrewrogers 2y agoTo some extent, yes. It creates issues for stack allocated types.
- slaymaker1907 2y agoYou can sort of work around it by placement new, but it’s still hugely inconvenient in the general case since the thing could have strict alignment requirements. Oh and not just stack, it also breaks global variables because destructors often get run for those when something gets unloaded or the program stops. Finally, because C++ gives you tons of control over how assignment works, it’ll probably break being able to use things within other objects unless you give those mark those classes as friend.
- IcyWindows 2y agoAh, I forgot that moved-from types in C++ still call the destructor.
- verdagon 2y agoThey almost work like this. std::move doesn't actually move an object; it leaves an intact instance... which we still don't know how to destroy (because its destructor is private). This also means that if we have an e.g. Outer struct whose constructor takes some kind of linear type, that Outer has no way to ever relinquish it again, which is a prerequisite of all those desirable linear type / higher RAII patterns. This godbolt hopefully illustrates: https://godbolt.org/z/qqMnnKbnd https://godbolt.org/z/qqMnnKbnd though apologies if it's not too clear. If C++ didn't have its quirks, you'd fundamentally be correct. Higher RAII can almost completely be implemented by this one sentence: make destruction private. (In fact, that's why Vale's `destruct` keyword is file-private)