back to index

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


Whisper Transcript | Transcript Only Page

00:00:00.000 | There's a lot of features that came in in C++11.
00:00:06.040 | There's a lot of features at the birth of C++ that were amazing and ideas with concepts
00:00:12.160 | in 2020.
00:00:14.560 | What to you is the most, just to you personally, beautiful or just you sit back and think,
00:00:26.120 | "Wow, that's just nice and clean feature of C++."
00:00:32.920 | I have written two papers for the History of Programming Languages Conference, which
00:00:39.520 | basically asked me such questions.
00:00:42.020 | And I'm writing a third one, which I will deliver at the History of Programming Languages
00:00:47.600 | Conference in London next year.
00:00:50.280 | So I've been thinking about that.
00:00:51.880 | And there is one clear answer, constructors and destructors.
00:00:56.560 | The way a constructor can establish the environment for the use of a type for an object, and the
00:01:04.800 | destructor that cleans up any messes at the end of it.
00:01:08.920 | That is key to C++.
00:01:11.000 | That's why we don't have to use garbage collection.
00:01:14.200 | That's how we can get predictable performance.
00:01:17.840 | That's how you can get the minimal overhead in many, many cases and have really clean
00:01:24.800 | types.
00:01:26.640 | It's the idea of constructor-destructor pairs.
00:01:30.840 | Sometimes it comes out under the name R-A-I-I.
00:01:36.080 | Resource acquisition is initialization, which is the idea that you grab resources in the
00:01:41.520 | constructor and release them in destructor.
00:01:44.720 | That's also the best example of why I shouldn't be in advertising.
00:01:48.480 | I get the best idea and I call it resource acquisition is initialization.
00:01:55.600 | Not the greatest naming I've ever heard.
00:02:00.280 | So it's types, abstraction of types.
00:02:07.080 | You said, I want to create my own types.
00:02:09.620 | So types is an essential part of C++ and making them efficient is the key part.
00:02:18.120 | And to you, this is almost getting philosophical, but the construction and the destruction,
00:02:25.000 | the creation of an instance of a type and the freeing of resources from that instance
00:02:32.660 | of a type is what defines the object.
00:02:36.880 | That's almost like birth and death is what defines human life.
00:02:42.280 | Yeah, that's right.
00:02:43.720 | By the way, philosophy is important.
00:02:46.120 | You can't do good language design without philosophy because what you are determining
00:02:52.080 | is what people can express and how.
00:02:55.320 | This is very important.
00:02:57.740 | By the way, constructors-destructors came into C++ in '79, in about the second week
00:03:04.720 | of my work with what was then called C++ classes.
00:03:08.600 | It is a fundamental idea.
00:03:11.380 | Next comes the fact that you need to control copying because once you control, as you said,
00:03:17.240 | birth and death, you have to control taking copies, which is another way of creating an
00:03:24.360 | object.
00:03:25.360 | And finally, you have to be able to move things around.
00:03:29.340 | So you get the move operations.
00:03:31.300 | And that's a set of key operations you can define on a C++ type.
00:03:37.920 | And so to you, those things are just a beautiful part of C++ that is at the core of it all?
00:03:47.760 | Thank you.
00:03:48.340 | Thank you.
00:03:49.420 | Thank you.
00:03:50.000 | Thank you.
00:03:50.500 | Thank you.
00:03:51.000 | Thank you.
00:03:51.500 | Thank you.
00:03:52.000 | Thank you.
00:03:52.500 | Thank you.
00:03:53.000 | Thank you.
00:03:53.500 | Thank you.
00:03:54.000 | Thank you.
00:03:54.500 | Thank you.
00:03:55.000 | Thank you.
00:03:55.500 | Thank you.
00:03:56.000 | Thank you.
00:03:56.500 | Thank you.
00:03:57.000 | Thank you.
00:03:57.500 | Thank you.
00:03:58.000 | Thank you.
00:03:58.500 | Thank you.
00:03:59.000 | Thank you.
00:03:59.500 | Thank you.
00:04:00.000 | Thank you.
00:04:00.500 | Thank you.
00:04:01.000 | Thank you.
00:04:01.500 | Thank you.
00:04:02.000 | Thank you.
00:04:02.500 | Thank you.
00:04:03.000 | Thank you.
00:04:03.500 | Thank you.
00:04:04.000 | [BLANK_AUDIO]