36 points · by beaukin · 4y ago · 59 comments
What would be a better terminology?
As I’ve been learning modern C++, I admit that I’ve struggled to understand why these terms make sense. To me, I think of it like “Resource Acquisition Promising Release”. Does this betray that I am not truly understanding Strourstrup’s principle?
RAII isn't about scope, it's about lifetimes that can persist beyond the instantiating scope. More like object-bound resource management.
"Lifetime" is a normative term in ISO C. In the 1999 standard, it is given as: "The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address, and retains its last-stored value throughout its lifetime." (6.4.2 Storage Durations of Objects)
"Lifetime" appears three times in the Index of the 1988 edition of Compilers: Principles, Techniques and Tools by Aho, Sethi and Ullman (a.k.a. the Red Dragon Book).
Lifetime, of a temporary 480
Lifetime, of an activation 391, 410
Lifetime, of an attribute 320-322, 324-329
``` (call-with-output-file some-file (lambda (out) (write 'hello out))) ```
scope(exit) foo(); // Call foo at the end of the scope
scope(success) foo(); //Call foo if all goes well
scope(failure) foo(); //Call foo if the wings fall off the plane.You can do
with lock:
# lock is held
# lock released
with lock:
# lock is held again
Also Go's defer keyword, which is bound to the current function (defer until function returns).For C++ and Rust there might not be a well defined scope since objects can be „moved“. Eg a method constructing an object can return it to the caller without the destructor being called.
The earliest mention of this idea (CADR / CADRe) I’ve found is from 2012: https://groups.google.com/a/isocpp.org/g/std-proposals/c/Una...
I even had my own proposal 5 years ago for improving that!
> That's right, but I would go one step further and have F(irst) and R(est), with obvious composition as follows: (using kruhft's examples from another thread)
> (ff x) == (caar x) == (car (car x))
> (rrf x) == (cddar x) == (cdr (cdr (car x)))
> I would argue that it's worth sacrificing the 'f' and 'r' symbols for such a common construct.> …
> Yes, I realize I'm 55 years late to the party.
https://news.ycombinator.com/item?id=13259344
Now we only need to implement both changes at the same time! :-P
Who is that for? Dummies who get it backwards? Oh, look, you're releasing in your destructor and acquiring in the destructor. Did you forget your CADR?
If the constructor throws an exception, the destructor is called immediately.
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++
Here's one important difference: Destructors work in standard data structures. If for whatever reason you want to build a map<string, list<fstream>>, 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#.
Yes, that means they hang around a lot longer, and that's sometimes problematic, but that's the GC way.
For resources like memory, where it’s merely a question of performance, I would agree. But some resources have correctness implications. Files, for example, you often want to close deterministically. It’s simply a terrible experience if a user can’t save because a file handle from a previous operation still lingers in a GC queue somewhere.
That’s also the reason why language features like try-with-resources were added in the first place.
I'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.
> Resource Acquisition Is Initialization or RAII, is a C++ programming technique[1][2] which binds the life cycle of a resource that must be acquired before use (allocated heap memory, thread of execution, open socket, open file, locked mutex, disk space, database connection—anything that exists in limited supply) to the lifetime of an object.
I’d crib the name from Rust, this is ownership, where the object owns the resource.
Also, see related video here
CppCon 2015: Andrei Alexandrescu “Declarative Control Flow"
https://youtu.be/WjTrfoiB0MQ?t=1046