This matches the case of a lexically scoped variable and no moves. With full RAII, the resources are tracked across their entire dynamic lifetime, not just one lexical block, allowing them to be returned to callers, placed in containers, etc.
Not really when taking heap into account, while having the code storage agnostic, which is why you need helper classes like handles or std::unique_ptr, with move semantics, otherwise the RAII story gets rather complicated.
In any case, you have two ways to do something similar, kind of.
Naturally there are multiple variations thereof, on how to handle errors, or which way to parameterise how to close resources.Then we have the C# inspired way,
And if open_resouce doesn't support using declaration, an helper builder function can be used to wrap it in such a type.Yes it isn't as automatic as pure RAII, but it gets close enough for most practical purposes.
Replies (2)
Replies (1)
Which besides not having global variables, also don't do continue, break, return.
The first approach traces back to how resource management was initially done in Lisp languages, alongside conditions.
Replies (1)