Search code examples
c++raii

Confusing idiom name RAII


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?


Solution

  • 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.