0

I recently learned about the wonderful memory management technique of RAII, which seems so much cleaner than the new/delete headache I learned in school years ago (I haven't looked at much C++ during the intervening years).

I'm trying to track down when this great technique was added to C++. Was it always there and I just missed the memo? What's the oldest version of the C++ standard which supports RAII?

[UPDATE: OK I realize now why this isn't an ideal SO question -- I have no practical way to verify whether any given answer is correct! Nonetheless I'm still very interested to learn the answer, and I assume the majority opinion will be true.

What I'm hearing is that it's always been there, which I don't doubt is true, but begs the question how come none of my old text books mention it? I just checked Meyers' Effective C++ 2nd edition which I have handy, and will check older texts tonight. Maybe the term was only recently coined, while the technique existed long before?]

Magnus
  • 10,736
  • 5
  • 44
  • 57
  • 4
    RAII is a concept, not a feature – aaronman Nov 01 '13 at 03:52
  • Well, all standardized versions support it. – chris Nov 01 '13 at 03:52
  • RAII is all about automatic deallocation/destruction. It's what happens when a stack variable goes out of scope and its destructor is called. This is pretty much at the heart of C++. It's always been there; but perhaps leveraging this feature of C++ as a usage pattern under the name of "RAII" came about later. – Anthony Nov 01 '13 at 03:55
  • It was added when template metaprogramming was. – ta.speot.is Nov 01 '13 at 04:00
  • @aaronman Doesn't RAII require special language-level support to invoke the destructor when the object goes out of scope? In which case, it's rightly a language feature, not just a concept? – Magnus Nov 01 '13 at 04:04
  • @Magnus the features are destructors combined with variables with automatic storage and the fact that destructors of those vars going out of scope are guaranteed to run(even when an exception is thrown). RAII itself is a concept (badly named IMO) that allows you to structure code to to be exception safe and provides many other benefits – aaronman Nov 01 '13 at 04:09
  • Guaranteed automatic destruction is a C++ feature. RAII is using guarantees like those to implement resource management contracts within a class. std::string does it - you don't have to call string.clear() to free the memory it's accumulated, it does it when it destructs. – kfsone Nov 01 '13 at 06:16

3 Answers3

3

When exactly the term "RAII" was coined, I'm not sure. But the technique itself existed in C++ from the moment of its invention, circa 1979.

The first version of the C++ ISO standard was published in 1998.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
2

Any language that has scoped variables with automatic destructor calls is able to do RAII. C++ had this concept from the very beginning as calling destructor when execution leaves scope where variable is declared was in C++ from the very beginning.

c-smile
  • 26,734
  • 7
  • 59
  • 86
2

Wikipedia's claim that Stroustrup invented RAII is backed by a reference to his book Design and Evolution of C++, published in 1994.

I don't have a copy to check, but it would seem to date from at least this time.

Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82