0

I think I understand what RAII means and I found numerous questions about this idiom on SO. My concern is more about the RAII name itself. I don't find a way to match the four words of this idiom to the concept it describes. Has someone has ever asked Bjarne about that?

In my understanding RAII means:

  • Encapsulate a Resource into a class.
    • The resource is Acquired in its constructor,
    • then released in its destructor.
  • This wrapper class must take care of the resource deletion when the object gets out of scope.

How can I explain to someone why RAII is named in such a way without giving the excuse that this isn't a good name for such a powerful idiom?

4
  • Why is that an issue? If it's just a bad name, no need to apologize too much about it, so long as you can explain the concept clearly :)
    – cigien
    Nov 11, 2020 at 13:10
  • To explain a concept, I should be able to explain why it is named that way. Otherwise, it would be a really bad explanation.
    – nowox
    Nov 11, 2020 at 13:11
  • The resource isn't encapsulated in a class, it's held by an object. Objects are initialized, so there you have your "is initialization". It's tied to object lifetime. I don't know why so many people think "classes" is the defining charectaristic of OOP. It's not. It's objects and their responsibilites. Nov 11, 2020 at 13:15
  • As you can see on wikipedia (RAII), that idiom has alternative names that you might prefer or explain better.
    – Jarod42
    Nov 11, 2020 at 16:19

3 Answers 3

4

In an RAII type, the constructor acquires the resource that needs to be managed. So, if you do

RAII_Type foo;

Then that resource acquisition happens at initialization, so resource acquisition is initialization.

2

Think about it this way: every Resource Acquisition must be an Initialization of an object, making the object responsible for the cleanup, not the initializer of the object. Resource acquisition without initialization is not allowed.

2

Resource Acquisition Is Inititialization

As mentioned earlier acquiring any resource involves initializing an object with the descriptor/handle/identifier of the resource. And the proper place to release the resource descriptor/handle/identifier - as a mandatory brace pair to the acquisition - is the object destructor. A well-designed class has fire-and-forget object instances; the user code need not worry about resource leaks, because the owning object is supposed to be in charge.

cheers, FM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.