Here's one important difference: Destructors work in standard data structures. If for whatever reason you want to build a map>, and the map goes out of scope, all files are correctly closed. (Rust's drop semantics work the same way.) That's a lot more work in Java or C#.
Ask HN: What should have been the term for RAII?
41–50 of 62 posts
Re: Ask HN: What should have been the term for RAII?
#42Constructor Acquires, Destructor Releases.
In C++, if the construction fails (with an exception), no other member function - not even the destructor - gets called and the resource is not acquired.
Re: Ask HN: What should have been the term for RAII?
#43Resource Is Getting Guaranteed to End up Destructed.
I try to have RIGGED as backronym, but the words are not completely right. Even so, the slogan could work like this: Prefer using RIGGED resources in C++
Re: Ask HN: What should have been the term for RAII?
#44I’m a Rust programmer but I would call it scope-based or scope-bound resource management, a popular alternative term in the C++ community.
RAII isn't about scope, it's about lifetimes that can persist beyond the instantiating scope. More like object-bound resource management.
Re: Ask HN: What should have been the term for RAII?
#45Re: Ask HN: What should have been the term for RAII?
#46I've tried reconciling how its defined here:
https://en.cppreference.com/w/cpp/language/initialization
However, it seems like wording describing ideas around how a resource is created, and not how it's completed.
Re: Ask HN: What should have been the term for RAII?
#47Earlier quoted context omitted.
C++ _isn't_ RAII. RAII is a design pattern you can apply, leveraging C++ language features, in C++ in certain cases to avoid a certain class of bugs.
Well sure but I would assume the pattern would be consistently implied in the standard library, no?
Re: Ask HN: What should have been the term for RAII?
#48The most important part for me is not construction,but the guaranteed destruction. So what about: Resource Is Getting Guaranteed to End up Destructed. I try to have RIGGED as backronym, but the words are not completely right. Even so, the slogan could work like this: Prefer using RIGGED resources in C++
Re: Ask HN: What should have been the term for RAII?
#49Earlier quoted context omitted.
In C++, if the construction fails (with an exception), no other member function - not even the destructor - gets called and the resource is not acquired.
This is one reason many people think exceptions in C++ are harmful, they cause unclear implicit behavior. Much better to handle a failure explicitly outside the constructor
If the constructor throws an exception, the destructor is called immediately.