Back to Index

Bjarne Stroustrup: Constructors, Destructors, and Resource Acquisition Is Initialization (RAII)


Transcript

There's a lot of features that came in in C++11. There's a lot of features at the birth of C++ that were amazing and ideas with concepts in 2020. What to you is the most, just to you personally, beautiful or just you sit back and think, "Wow, that's just nice and clean feature of C++." I have written two papers for the History of Programming Languages Conference, which basically asked me such questions.

And I'm writing a third one, which I will deliver at the History of Programming Languages Conference in London next year. So I've been thinking about that. And there is one clear answer, constructors and destructors. The way a constructor can establish the environment for the use of a type for an object, and the destructor that cleans up any messes at the end of it.

That is key to C++. That's why we don't have to use garbage collection. That's how we can get predictable performance. That's how you can get the minimal overhead in many, many cases and have really clean types. It's the idea of constructor-destructor pairs. Sometimes it comes out under the name R-A-I-I.

Resource acquisition is initialization, which is the idea that you grab resources in the constructor and release them in destructor. That's also the best example of why I shouldn't be in advertising. I get the best idea and I call it resource acquisition is initialization. Not the greatest naming I've ever heard.

So it's types, abstraction of types. You said, I want to create my own types. So types is an essential part of C++ and making them efficient is the key part. And to you, this is almost getting philosophical, but the construction and the destruction, the creation of an instance of a type and the freeing of resources from that instance of a type is what defines the object.

That's almost like birth and death is what defines human life. Yeah, that's right. By the way, philosophy is important. You can't do good language design without philosophy because what you are determining is what people can express and how. This is very important. By the way, constructors-destructors came into C++ in '79, in about the second week of my work with what was then called C++ classes.

It is a fundamental idea. Next comes the fact that you need to control copying because once you control, as you said, birth and death, you have to control taking copies, which is another way of creating an object. And finally, you have to be able to move things around. So you get the move operations.

And that's a set of key operations you can define on a C++ type. And so to you, those things are just a beautiful part of C++ that is at the core of it all? Yes. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you.

Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you.