4 ms·
RAII was created as a solution to the problem of C++ code not releasing its resources when an exception happened. By putting the resource release in a destructo
by zik 8y ago
RAII was created as a solution to the problem of C++ code not releasing its resources when an exception happened. By putting the resource release in a destructor this is fixed through piggybacking on C++'s semantic of calling destructors when objects go out of scope, even through exceptions.
To put it another way, RAII is a hack which co-opts one semantic to cover the drawbacks of another semantic. Without exceptions there's no need for RAII because there's no problem to solve.
- jjnoakes 8y agoRAII isn't only about exceptions (although it helps greatly); see Rust for example. RAII is about any scope exit automatically cleaning up resources. Exceptions is a big one, but so is return, break, etc. Having it happen automatically in all cases is a win for robust software (in my opinion).