4 ms·
> But since in most cases I don't care about destruction so long as it happens But you might want to care about where in the code the destruction happens. Thi
by zozbot234 3y ago
> But since in most cases I don't care about destruction so long as it happens
But you might want to care about where in the code the destruction happens. This is quite normal in a low level language. Of course I do agree that it should be easy to avoid leaking resources, but static analysis can ensure this in most cases as you point out.
- bluGill 3y agoRAII always gives me cleanup at the last possible moment. Sometimes I might want to force the cleanup sooner, but later than this always means never which is not what I want.
- LegionMammal978 3y agoThat's indeed one of the downsides of RAII in C++ (unless the type supports zero-cost moved-from objects), but it's not a universal issue. Rust's implementation lets you transfer ownership of an object ("move" it) from one variable to another. Afterward, the old variable will be uninitialized, and the new variable will hold the object, with its scope determining when the object is destroyed. If the new variable has a smaller scope, then the object will be destroyed sooner than it would have been otherwise. In particular, to assist in early cleanup, the standard library has a drop(value) function, that takes ownership of any value and immediately lets it fall out of scope, destroying it. This is especially useful for things like destroying RAII mutex guards to immediately unlock the corresponding mutex.