Skip to content
Bjarne Stroustrup: C++ | Lex Fridman Podcast #48
This video isn’t embeddableWatch on YouTube →
Lex Fridman PodcastLex Fridman Podcast

Bjarne Stroustrup: C++ | Lex Fridman Podcast #48

Bjarne Stroustrup is the creator of C++, a programming language that after 34 years is still one of the most popular and powerful languages in the world.

Lex FridmanhostBjarne Stroustrupguest
Nov 7, 20191h 47mWatch on YouTube

Summaries, chapters, transcripts, and FAQs on this page are made by uListen, not YouTube.

Partial transcript

0:00 – 1:40

Introduction

Lex Fridman 0:00 The following is a conversation with Bjarne Stroustrup. He's the creator of C++, a programming language that after 40 years is still one of the most popular and powerful languages in the world. Its focus on fast, stable, robust code underlies many of the biggest systems in the world that we have come to rely on as a society. If you're watching this on YouTube, for example, many of the critical backend components of YouTube are written in C++. Same goes for Google, Facebook, Amazon, Twitter, most Microsoft applications, Adobe applications, most database systems, and most physical systems that operate in the real world, like cars, robots, rockets that launch us into space and one day will land us on Mars. C++ also happens to be the language that I use more than any other in my life. I've written several hundred thousand lines of C++ source code. Of course, lines of source code don't mean much, but they do give hints of my personal journey through the world of software. I've enjoyed watching the development of C++ as a programming language, leading up to the big update in the standard in 2011 and those that followed in '14, '17 and toward the new C++ 20 standard hopefully coming out next year. This is The Artificial Intelligence Podcast. If you enjoy it, subscribe on YouTube, give it five stars on iTunes, support it on Patreon, or simply connect with me on Twitter @lexfridman, spelled F-R-I-D-M-A-N. And now, here's my conversation with Bjarne Stroustrup.

1:40 – 2:18

First program

Lex Fridman 1:40 What was the first program you've ever written? Do you remember?

Bjarne Stroustrup 1:45 It was my second year in university, first year of computer science. And it was in Algol 60. I calculated the shape of a super ellipse and then connected points on the, on the perimeter, uh, creating star patterns. It was with a- w- with a wet ink on a paper printer. (laughs)

Lex Fridman 2:11 And that was in college, in university?

Bjarne Stroustrup 2:13 Yeah, yeah. I learned to program the second year in university.

2:18 – 16:45

Journey to C++

Lex Fridman 2:18 And what was the first programming language, if I may ask it this way, that you fell in love with?

Bjarne Stroustrup 2:26 I, I think Algol 60. And after that, I remember... I remember SNOBOL. I remember Fortran, didn't fall in love with that. I remember Pascal, didn't fall in love with that. It all got in the way of me. Uh, and then I discovered Assembler and that was much more fun. And from there, I went to micro, um, microcode.

Lex Fridman 2:54 So you were drawn to the... You found the low-level stuff beautiful.

Bjarne Stroustrup 3:01 I, I went through a lot of languages and then I spent significant time in, in Assembler and microcode. That was sort of the first really profitable things and that paid for my master's actually. And then I discovered Simula, which was absolutely great.

Lex Fridman 3:19 Simula?

Bjarne Stroustrup 3:20 Simula was the extension of Algol 60, uh, done primarily for simulation, but basically they invented object-oriented programming and inheritance and runtime polymorphism when they were, while they were doing it. And, uh, that was a language that taught me that you could have the, sort of the problems of a program grow with size of the program rather than with the square of the size of the program. That is, you can actually modularize very nicely. And that, that, that was a surprise to me. It was also a surprise to me that a stricter type system than Pascal's was helpful, whereas Pascal's type system got in my way all the time. So you need a, a strong type system to organize your code well, but it has to be extensible and flexible.

Lex Fridman 4:21 Let's get into the details a little bit. What kind... If you remember, what kind of type system did Pascal have? What type system, typing system did Algol 60 have?

Bjarne Stroustrup 4:31 Basically, Pascal was sort of the simplest language that Niklaus Wirth could define that served the needs of Niklaus Wirth at the time, and it- it has a sort of a highly, uh, moral tone to it. That is, if you can say it, uh, in Pascal, it's good, and if you can't, it's not so good. Whereas Simula allows you basically to build your own type system. So instead of trying to fit yourself into, uh, Niklaus Wirth's world, Kristen Nygaard's language and Ole Johan Dahl's language allowed you to build your own. So it's sort of close to the original idea of, of you- you- you build a domain-specific language. As a matter of fact, what you build is a set of types and relations among types that allows you to express, uh, something that's suitable for an application.

Lex Fridman 5:36 So when you say types, stuff you're saying has echoes of object-oriented programming. (laughs) So-

Bjarne Stroustrup 5:42 Yes, they invented it. Every language that uses the word "class" for type is a descendant of Simula. Directly or indirectly. Kristen Nygaard and Ole Johan Dahl were mathematicians, um, and they didn't think in terms of types, they, um, but they understood sets and classes of, uh, elements and so they called their types classes. And basically, in C++, as in Simula, a class is a user-defined type.

Lex Fridman 6:18 So can you try the impossible task and give a brief history of programming languages from your perspective? So we started with ALGOL 60, Simula, Pascal, but that's-

Bjarne Stroustrup 6:31 I- I- I can-

Lex Fridman 6:31 ... just the '60s and '70s.

Bjarne Stroustrup 6:33 ... I can try. The most sort of interesting and major improvement of programming languages was, um, Fortran, the first Fortran. Because before that, all code was written for a specific machine and each, uh, specific machine had a language, assembly language or, uh, microassembler or some extension of that idea. But it- you are writing for a specific machine in the ter- in the language of that machine.

Lex Fridman 7:06 Mm-hmm.

Bjarne Stroustrup 7:07 And Backus and his team at IBM built a language that would allow you to, to write what you really wanted. That is, y- you could write it in a language that was natural for people. Now, these people happened to be engineers and physicists, so the language that came out was somewhat unusual for the rest of the world. But basically, they said formula translation because they wanted to have the mathematical formulas translated into the machine.

Lex Fridman 7:38 Mm-hmm.

Bjarne Stroustrup 7:38 And as a side effect, they got portability, because now they're writing in the terms that the humans use and the way humans thought, and then they had a program that translated it into the machine's needs. And, and that was new, and that was, uh, great, and it's something to, to remember. We want to raise the language to the human level, but we don't want to lose the efficiency. So-

Lex Fridman 8:10 And that was the first step towards the human?

Bjarne Stroustrup 8:12 That was the first step, um, and of course, they were very particular kind of humans. Businesspeople-

Lex Fridman 8:19 Mathematicians.

Bjarne Stroustrup 8:19 ... were different, so they got COBOL instead and-

Lex Fridman 8:22 (laughs)

Bjarne Stroustrup 8:22 ... et cetera, et cetera. And Simula came out... Uh, no, let's not go to Simula yet. Let's go to ALGOL. Uh, Fortran didn't have, at the time, the notions of... not a precise notion of type, not a precise notion of scope, uh, not a, um, a, a set of translation phases that was, uh, what we have today, lexical, syntax, semantics. It was sort of a bit of a model in the early days, but hey, they'd just done the biggest, uh, breakthrough in the history of programming, right? So you can't criticize them for not having gotten all the technical details right. So we got ALGOL. That was, uh, very pretty, and most people in commerce and science considered it useless because it was not flexible enough and it wasn't efficient enough and et cetera, et cetera. Uh, but that was a breakthrough from a technical point of view. Then Simula came along to make that idea more flexible, and you could define your own types, and that's w- where, where I got very interested. Kristen Nygaard, who's the main idea man behind, uh, Simula-

Lex Fridman 9:44 That was late '60s.

Bjarne Stroustrup 9:45 This was late '60s. Well, I was a visiting professor in, uh, Aarhus, and so I learned object-oriented programming by sitting around and, well, in theory, discussing with Ole Ju- uh, with, with Kristen Nygaard. But Kristen, once he gets started i- in, in full flow, it's very hard to get a word in edgeways with.

Lex Fridman 10:10 You're just listening.

Bjarne Stroustrup 10:10 So, um, it, it was great. I learned it from there.

Lex Fridman 10:14 Not to romanticize the notion, but it seems like a big leap to think about o- object-oriented programming. W- uh, it's, it's really a leap of abstraction. It's-

Bjarne Stroustrup 10:25 Yes.

Lex Fridman 10:26 And was that as, uh, big and beautiful of a leap as it seems from now in retrospect, or was it an obvious one at the time?

Bjarne Stroustrup 10:38 It was not obvious, and many people have tried to do something like that, and most people didn't come up with something as, as wonderful as Simula. Uh, lots of people got their PhDs and made their careers out of, um, forgetting about Simula or never knowing it. For, for me, the key idea was basically I could get my own types, and that's the idea that goes further into C++, where I can get, uh, better types and more flexible types and more efficient types. But it's still the fundamental idea when I want to write a program, I want to write it with my types that is appropriate to my problem-

Lex Fridman 11:21 Mm-hmm.

Bjarne Stroustrup 11:22 ... uh, and under the constraints that I'm under with hardware, software, environment, et cetera. And tha- that's, that's the key idea. People picked up on the class hierarchies and the virtual functions and the inheritance, and that was only part of it. It was an interesting and major part and still a major part in a lot of graphics stuff, but it was not the most fundamental. It, it was when you wanted to relate one type to another, you don't want them all to be independent. The, the classical example is that you don't actually want to write a city simulation with vehicles where you say, "Well, if it's a bicycle, uh, write the code for turning a bicycle to the left. If it's a normal car, turn right the normal car way. If it's a fire engine, turn right the fire engine way," da-da-da-da-da-da. You get these big case statements and-... bunches of if statements and such. Instead, you, you tell the, uh, the, the, the base class, the, that, uh, uh, that's the vehicle and saying, "Turn, turn left the way you want to."

Lex Fridman 12:38 (laughs) Yeah.

Bjarne Stroustrup 12:39 And this is actually a real example. Uh, they, they used it to simulate, um, and optimize the emergency-

Lex Fridman 12:48 Traffic flow or something like that.

Bjarne Stroustrup 12:48 ... the, the emergency services for, uh, somewhere in Norway, uh, back in the '60s.

Lex Fridman 12:55 Wow.

Bjarne Stroustrup 12:55 So this was one of the early examples for why you needed inheritance and, and you needed, um, a runtime polymorphism, uh, s- because you wanted to handle this set of, um, of vehicles in a manageable way. You, y- y- you can't just rewrite your code each time a new kind of vehicle comes along.

Lex Fridman 13:20 Yeah, that's a beautiful, powerful idea. And, of course, it stretches through your work with C++ as we'll talk about. But I think you've structured it nicely. Um, what other breakthroughs came along in the history of programming languages, if, if we were to tell the history-

Bjarne Stroustrup 13:38 Yeah.

Lex Fridman 13:38 ... in that way?

Bjarne Stroustrup 13:39 Obviously, I'm better telling the part of the history that, that is the path I am on as opposed to, to all the paths.

Lex Fridman 13:46 Yeah, you skipped the hippie, uh, uh, John McCarthy and Lisp-

Bjarne Stroustrup 13:50 Yeah.

Lex Fridman 13:50 ... one of my favorite languages.

Bjarne Stroustrup 13:52 Um, but Lisp is-

Lex Fridman 13:53 Function.

Bjarne Stroustrup 13:53 But Lisp is not one of my favorite languages.

Lex Fridman 13:56 Yes.

Bjarne Stroustrup 13:56 It's obviously important. It's obviously interesting. Lots of people write code in it, and then they rewrite it into C or C++ when they want to go-

Lex Fridman 14:06 (coughs)

Bjarne Stroustrup 14:06 ... to production.

Lex Fridman 14:07 Yes.

Bjarne Stroustrup 14:07 It's in the world I'm at, which are constrained by performance, reliability, um, issues, deployability, cost of hardware. Um, I, I don't like things to be too dynamic.

Lex Fridman 14:25 Mm-hmm.

Bjarne Stroustrup 14:26 It is really hard to write a piece of code that's perfectly flexible that you can also deploy on a small computer and that you can also put in, say, a telephone switch in, uh, Bogota. What's the chance if you get an error and you find yourself in the debugger that the telephone switch in Bogota on late Sunday night has a programmer around?

Lex Fridman 14:51 Right.

Bjarne Stroustrup 14:52 The chance is zero. And so a lot of things I think most about can't afford that flexibility. Um, I'm quite aware that maybe 70, 80% of all code are not under the kind of constraints I'm interested in. But somebody has to do the, the, the job I'm doing, uh, because you have to get from these high-level flexible languages to the hardware.

Lex Fridman 15:23 The stuff that'll last for 10, 20, 30 years is robust-

Bjarne Stroustrup 15:27 Yeah.

Lex Fridman 15:27 ... operates under very constrained conditions. Yes. Absolutely.

Bjarne Stroustrup 15:31 That's right.

Lex Fridman 15:31 And it's fascinating and beautiful in its own way. It's, uh, C++ is-

Bjarne Stroustrup 15:36 Yeah.

Lex Fridman 15:36 ... uh, one of my favorite languages and so is Lisp. So I can-

Bjarne Stroustrup 15:40 Yeah.

Lex Fridman 15:40 ... I can embody two for different reasons-

Bjarne Stroustrup 15:43 Yeah.

Lex Fridman 15:43 ... uh, as, um, as a programmer.

Bjarne Stroustrup 15:47 I, I understand why Lisp is popular and I can see, uh, the beauty of the ideas and similarly with, um, with Smalltalk.

Lex Fridman 15:56 There's more to talk and all the functional languages.

Bjarne Stroustrup 15:57 It's just not as relative lang- it is not as relevant in my world. And by the way, I distinguish between those and the functional languages where I go to things like ML and Haskell.

Lex Fridman 16:12 Haskell.

Bjarne Stroustrup 16:13 Uh, different, different kind of languages. Uh, they have a different kind of beauty and they're very interesting. And I actually try to learn from all the languages I encounter to see what is there that would make working on the kind of problems I'm interested in with the kind of constraints, um, that, that I'm interested in, wha- what can actually be done better?

Lex Fridman 16:40 Mm-hmm.

Bjarne Stroustrup 16:41 Because sh- we can surely do better than we do today.

16:45 – 23:20

Learning multiple languages

Lex Fridman 16:46 You've, uh, you've said that it's good for any professional programmer to know at least five languages. That's speaking about a variety of languages that you've taken inspiration from. And you've listed th- yours as being, uh, at least at the time, C++, obviously, Java, Python, Ruby, and JavaScript. Can you, first of all, update that list, modify it? Uh, you don't have to be constrained to just five, but can you describe what you picked up also from each of these languages, how you see them as inspirations for even your work in, with C++?

Bjarne Stroustrup 17:26 This is a very hard question to answer. So about languages, you should know languages. I, I reckon I knew about 25 or thereabouts when I did C++. It was easier in those days because the languages were smaller and, uh, you didn't have to learn a whole programming environment and such to do it. You, you could learn a language quite easily. And, uh, it's, it's good to learn so many languages and-

Lex Fridman 17:56 I imagine just like with, uh, natural language for communication, there's different paradigms that emerge in all of them.

Bjarne Stroustrup 18:05 Yeah.

Lex Fridman 18:06 That there's commonalities and so on.

Bjarne Stroustrup 18:09 So I picked five out of a hat as a number.

Lex Fridman 18:11 You picked five out of a hat. Let's look at languages-

Bjarne Stroustrup 18:12 Obviously. The important thing that the number is not one.

Lex Fridman 18:17 (laughs) That's right.

Bjarne Stroustrup 18:18 Um, it's like, I don't like ... I mean, if you are monoglot, you are likely to think that your own culture is the only one that's superior to everybody else's. A good learning of a foreign language and a foreign culture is important. It helps you think and be a better person. With programming languages, you become a better programmer, a better designer with a second language. Now, once you've got two, the way to five is not-... that long. It's the second one that's most important. And then when I had to pick five, um, I sort of thinking, "What kinds of languages are there?" Well, there's a really low-level stuff. It's good, it's actually good to know machine code. Most pe-

Lex Fridman 19:05 Even still? Sorry to interrupt but-

Bjarne Stroustrup 19:06 Even today, even today.

Lex Fridman 19:07 Even today?

Bjarne Stroustrup 19:08 Um, the C++ optimizers write better machine code than I do.

Lex Fridman 19:13 Yes.

Bjarne Stroustrup 19:14 But I don't think I could appreciate them if I actually didn't understand, uh, machine code and machine architecture. At least in, in, in my position, I have to understand a bit of it because (sighs) you mess up the cache and you're off in performance by a factor of 100. Right? It shouldn't be that if you are interested in either performance or the size of the computer you have to deploy. Uh, so, so I would go S assembler. Uh, I used to mention C, but these days going low level is not actually what gives you the performance.

Lex Fridman 19:53 (laughs)

Bjarne Stroustrup 19:53 It is to express your ideas so cleanly that you can think about it and the optimizer can understand what you're up to. My favorite way of optimizing these days is to throw away... out the clever bits and see if it still runs fast. And sometimes it runs faster.

Lex Fridman 20:12 (laughs)

Bjarne Stroustrup 20:12 Um, so I need the abstraction mechanisms or something like C++ to write compact high-performance code. Uh, there was a beautiful keynote by Jason Turner at the CppCon a couple of years ago where he decided he was going to program, uh, Pong on, um, um, Motorola 60, uh, 800 I think it was. And he says, "Well, this is relevant because it looks like a microcontroller. It has specialized hardware, it has not very much memory and it's relatively slow." And so he shows in real time how he writes Pong starting with fairly straightforward low-level stuff, improving his abstractions and what he's doing, he's writing C++ and it translate into, into 86 assembler, which, uh, you can do with, with Clang and you can see it in real time. It's, um, the compiler explorer, which you can use on the web. And then he wrote a little program that translated 86 assembler into Motorola assembler.

Lex Fridman 21:25 Mm-hmm.

Bjarne Stroustrup 21:25 And so he types and you can see this thing on-

Lex Fridman 21:28 In, in real time? Wow.

Bjarne Stroustrup 21:29 You can see it in real time and even if you can't read the assembly code, you can just see it... his code gets better, the code g- uh, the assembler gets smaller. He increases the abstraction level, uses C++11, as it were, better. This code gets cleaner, it gets easier, maintainable and the code shrinks.

Lex Fridman 21:50 Wow.

Bjarne Stroustrup 21:50 And it keeps shrinking.

Lex Fridman 21:52 Wow.

Bjarne Stroustrup 21:53 And I could not in any reasonable amount of time write that assembler as good as the compiler generated from really quite nice modern C++. And, and I'll go as far as to say the, the thing that looked like C was significantly, uh, uglier and, and smaller when it became s- me- uh, and, and larger when it became machine code. So up the, the abstractions that can be optimized are important.

Lex Fridman 22:27 I would love to see that kind of visualization in larger code bases.

Bjarne Stroustrup 22:31 Yeah.

Lex Fridman 22:31 That might be beautiful.

Bjarne Stroustrup 22:32 But you can't show a larger code base in a one-hour talk and ha- have it fit on screen.

Lex Fridman 22:38 Right. So that's C and C++?

Bjarne Stroustrup 22:39 If you... so, uh, my two languages would be machine code and C++.

Lex Fridman 22:43 Right.

Bjarne Stroustrup 22:44 And then I think you can learn a lot from the functional languages. So pick, pick Haskell or ML, I don't care which. I think actually you, you'll, you'll learn the same lessons of, uh, expressing especially mathematical notions really clearly and having the, the, the, a, a type system that's really strict. And then you should probably have a language for sort of quickly churning out, uh, something. Uh, you could pick JavaScript, you could, uh, pi- pick Python, you could pick Ruby.

23:20 – 25:08

Javascript

Lex Fridman 23:20 What do you make of JavaScript in general? So you kind of... you're talking in the platonic sense about languages, about what they're good at, what their philosophy of design is. But there's also a large user base behind each of these languages and they use it in a way sometimes maybe it wasn't really designed for.

Bjarne Stroustrup 23:39 That's right.

Lex Fridman 23:40 JavaScript is used way beyond, uh, probably what it (laughs) was designed for.

Bjarne Stroustrup 23:43 L- l- let me say it this way. When you build a tool, you do not know how it's going to be used. You try to improve the tool by looking at how it's being used and when people cut their fingers off and try and stop that from happening.

Lex Fridman 23:58 (laughs) Yeah.

Bjarne Stroustrup 23:58 Um, but really you have no control over how something is used. So I'm very happy and proud of some of the things C++ being used at and some of the things I wish people wouldn't do. Bitcoin mining being my favorite example. Uses as much energy as Switzerland and, uh, mostly serves, uh, criminals.

Lex Fridman 24:20 Yeah.

Bjarne Stroustrup 24:21 But back to, back to the languages. I actually think that having JavaScript run in the browser wa- wa- was, w- was an enabling thing for a lot of things. Yes, you could have done it better, but people were trying to do it better and they were using pre- uh mm- sort of more principled language designs, but they just couldn't do it right. And the-... non-professional programmers that write lo- lots of that code just couldn't understand them. So let, it, it did a, an amazing job for, for what it was. It's not the prettiest language and I don't think it ever will be, uh, the prettiest language, but let's not be bigots here.

Lex Fridman 25:07 So

25:08 – 31:53

Efficiency and reliability in C++

Lex Fridman 25:08 what was the origin story of C++?

Bjarne Stroustrup 25:12 Yeah.

Lex Fridman 25:12 You, you basically gave a few perspectives of your inspiration of object-oriented programming. That's ... You had a connection with C and performance. Efficiency was an important, uh, thing you were drawn to.

Bjarne Stroustrup 25:28 Efficiency and reliability.

Lex Fridman 25:30 Reliability, yeah.

Bjarne Stroustrup 25:30 You have to get both.

Lex Fridman 25:32 What, what, what's reliability?

Bjarne Stroustrup 25:35 I, I, I really want my telephone calls to get through and I want the quality of what I am talking coming out at the other end. The other end might be in London or wherever. Um, so ... And y- and you don't want the system to be crashing. If you're doing a bank, uh, it's, uh, you mustn't crash. It might be your, uh, uh, your bank account that is in trouble. There's different constraints. Like, in games, it doesn't matter too much if there's a crash. Nobody dies and nobody gets ruined. But I- I'm interested in the combination of performance, uh, partly because of sort of s- speed of things being done, part of being able to do things that is necessary to, to, to have reliability, uh, of larger systems. If you spend all your time, um, interpreting a, a simple function call, you are not going to have enough time to do proper signal processing to get the telephone calls to sound right. Um, either that or you have to have 10 times as many computers and you can't afford your phone anymore. It's a ridiculous idea, uh, in the modern world because we have solved all of those problems.

Lex Fridman 26:55 I mean, they keep popping up in different ways.

Bjarne Stroustrup 26:58 Yeah.

Lex Fridman 26:58 We ... 'Cause we ta- tackle bigger and bigger problems, so efficiency remains (laughs) always an important-

Bjarne Stroustrup 27:02 Yeah.

Lex Fridman 27:02 ... uh, aspect.

Bjarne Stroustrup 27:03 But you have to think about efficiency not just as speed, but as an enabler to, uh, important things. And one of the things it enables is, uh, is reliability, is dependability. You ... When, when I press the pedal, uh, the brake pedal of a car, it is not actually connected directly to, uh, to anything but a computer.

Lex Fridman 27:28 Yeah.

Bjarne Stroustrup 27:28 That computer better work.

Lex Fridman 27:31 Let's talk about reliability just a little bit. So modern cars have ECUs, have millions of lines of code-

Bjarne Stroustrup 27:40 Mm-hmm.

Lex Fridman 27:40 ... today. So this is certainly especially true of autonomous vehicles where some of the aspect of the control or driver assistance systems that steer the car to keep it in the lane and so on.

Bjarne Stroustrup 27:51 Mm-hmm.

Lex Fridman 27:52 So how do you think ... You know, I talk to regulators, people in government, who are very nervous about testing the safety of these systems-

Bjarne Stroustrup 28:00 Mm-hmm.

Lex Fridman 28:00 ... of so- of software, ultimately software that makes decisions that could lead to fatalities. So h- how do you ... How do we test software systems like these?

Bjarne Stroustrup 28:13 First of all, safety, like performance and like, uh, security, is a systems property. People tend to look at one part of a system at a time and saying something like, "This is secure. That's all right. I don't need to do that. Yeah, that piece of code is secure. I'll buy your operator."

Lex Fridman 28:37 Right.

Bjarne Stroustrup 28:38 If you want to have reliability, if you want to have performance, if you want to have security, you have to look at the whole system.

Lex Fridman 28:47 I did not expect you to say that, but that's very true. Yes.

Bjarne Stroustrup 28:50 I'm dealing with one part of the system and I want my part to be really good, but I know it's not the whole system. Furthermore, e- making an individual part perfect may actually not be the best way of getting the highest degree of reliability and performance and such. There's people who say, "C++ is type safe, uh, not type safe. You can break it." Sure. I can break anything that runs on a computer. I may not go through your type system. If I wanted to break into your computer, I'll probably try SQL injection.

Lex Fridman 29:28 And it's very true. If you think about safety or even reliability at a systems level, especially when a human being is involved, it's ... starts becoming hopeless pretty quickly in terms of, uh, proving that something is safe to a certain level-

Bjarne Stroustrup 29:49 Yeah.

Lex Fridman 29:49 ... 'cause there's so many variables. It's so complex.

Bjarne Stroustrup 29:52 Well, let's get back to something we can talk about-

Lex Fridman 29:54 (laughs)

Bjarne Stroustrup 29:55 ... and, and actually make some progress on.

Lex Fridman 29:57 Yes.

Bjarne Stroustrup 29:58 Uh, we, we could look at C++ programs and we can try and make sure they, um, crash less often.

Lex Fridman 30:07 Yeah.

Bjarne Stroustrup 30:07 The way you do that-

Lex Fridman 30:08 Mm-hmm.

Bjarne Stroustrup 30:09 ... is largely by simplification. It is not ... The first step is to simplify the code, have less code, have code that are less likely to go wrong. It's not by runtime testing everything. It is not by, um, big test frameworks that you are using. Yes, we do that also. But the first step is actually to make sure that when you want to express something, you can express it directly in code rather than going through endless loops and convolutions in your head before it gets down the code.... that if, if, if the way you are thinking about a problem is not in the code-

Lex Fridman 30:54 Mm-hmm.

Bjarne Stroustrup 30:55 ... there is a missing piece that's just in your head. And the code, you can see what it does, but it cannot see what you thought about it, unless you have expressed things directly. When you express things directly, you can maintain it. It's easier to find errors. It's easier to make modifications. It's actually easier to test it. And lo and behold, it runs faster, and therefore, you can use a, a smaller number of computers, which means there's less hardware that could possibly break. Um, so I think the key here is simplification, but it has to be, um, to use the Einstein quote, "As simple as possible and no simpler."

Lex Fridman 31:40 No simpler. But how do you-

Bjarne Stroustrup 31:41 There are other areas with under constraints where you can be simpler than you can be in C++. But in the domain I'm dealing with, that's the simplification I'm after.

31:53 – 36:45

What does good code look like?

Lex Fridman 31:53 So how do you inspire or ensure that, uh, the Einstein level of simplification is reached? So c- can you do code review? Can you look at code? Is there... If I gave you the code for the Ford F-150 and said, "Here." (laughs) Is this a mess or is this okay? Is it possible to tell? Is it possible to regulate?

Bjarne Stroustrup 32:23 An experienced developer can do a code and see if it smells.

Lex Fridman 32:29 (laughs)

Bjarne Stroustrup 32:29 And, uh, I mix metaphors deliberately.

Lex Fridman 32:31 Yes.

Bjarne Stroustrup 32:32 Uh, the, the point is that it is hard to generate something th- that is really obviously clean and, uh, um, can be appreciated. But you can usually recognize when you haven't reached that point. And so, i- if I... I've never looked at, uh, the F150 code, so I wouldn't know, but I know what I would be looking for. There... I'd be looking for some tricks that correlates with bugs and elsewhere and, uh, I, I have tried to formulate, uh, rules for what good code looks like. Um, and the, the current, uh, version of that is called the C++ Core Guidelines. One thing people should remember is there's what you can do in a language and what you should do. In a language, you have lots of things that is necessary in some context, but not in others. There's things that exist just because there's 30-year-old code out there and you can't get rid of it. But you can have rules that says, "When you create it, try and follow these rules." This does not create good programs by themselves, but it limits the damage and, uh, o- from mistakes, it limits the possibilities of the mistakes. And basically, we are trying to say, what is it that a good programmer does-

Lex Fridman 34:12 Mm.

Bjarne Stroustrup 34:12 ... at the fairly simple level of where you use the language and how you use it? Now, I can put all the rules for chiseling in mar- marble. It doesn't mean that somebody who follows all of those rules, uh, can do a masterpiece by Michelangelo. That is, there's something else to write a good program, just as there's something else to create a, a important work of art. That is, there's a... There's some kind of inspiration, understanding, gift. But we can approach the sort of technical, the craftsmanship level of it. The, the, the famous painters, the famous cultures was, among other things, superb craftsmen. They could express their ideas using, um, their tools very well. And so these days, I think what I'm doing, what a lot of people are doing, we are still trying to figure out how it is to use our tools very well. For a really good piece of code, you, you need a spark of inspiration and you can't, I think, regulate that. You, you, you cannot say that, um, "I'll take a picture only... Uh, I'll buy your picture only if, uh, you're at least van Gogh." Um-

Lex Fridman 35:45 (laughs)

Bjarne Stroustrup 35:45 ... there are other things you can regulate but, but not the inspiration.

Lex Fridman 35:50 I think that's quite beautifully put. It is true that there is... As, as an experienced programmer, when you see code that's inspired, that's, uh, like Michelangelo, you know it when you see it. And, uh, the opposite of that is code that is messy, code that smells, you know when you see it. And I'm not sure you can describe it in words, except vaguely through guidelines and so on.

Bjarne Stroustrup 36:20 Yes. It's easier to recognize ugly than to recognize beauty in code. And for the reason is that sometimes beauty comes from something that's innovative and unusual, and you have to sometimes think reasonably hard to appreciate that. On the other hand, a me- uh, the messes have things in common.

Lex Fridman 36:43 (laughs)

Bjarne Stroustrup 36:44 And

36:45 – 41:16

Static checkers

Bjarne Stroustrup 36:45 you can... You can have static checkers and dynamic checkers that finds, uh, a large number of the-... most common mistakes. Uh, you can catch a lot of sloppiness mechanically. I'm a great fan of static analysis, in particular, uh, because you can check for not just the language rules, but for the usage of language rules. And I think we will see much more static analysis in the coming decade. We-

Lex Fridman 37:19 Can you describe what static analysis is?

Bjarne Stroustrup 37:21 You represent a piece of code so that you can write a program that goes over, uh, that representation and look for things that are, um, right and not right. So for instance, you can an- an- analyze a, a program to see if, um, resources are leaked. That's one of my favorite, uh, problems. It's not actually all that hard in modern C++, but you can do it. If you're writing in the C level, you have to have a malloc and a free, and, uh, they have to match. If you have them in a single function, you can usually do it very easily. If there's a, um, malloc here, there should be a free there. On the other hand, in between can be true in complete code, and then-

Lex Fridman 38:17 (laughs)

Bjarne Stroustrup 38:18 ... it becomes impossible.

Lex Fridman 38:19 Yeah.

Bjarne Stroustrup 38:19 If you pass that pointer to the, uh, memory out of a function and then want to make sure that the free is done, uh, somewhere else, now it gets really difficult. And so-

Lex Fridman 38:33 Mm-hmm.

Bjarne Stroustrup 38:34 ... for static analysis, you can run through a program, and you can try and figure out if there's any leaks. And what you'll probably find is that you will find some leaks, and you'll find quite a few places where your analysis can't be complete. It might depend on runtime. It might depend on, um, the, the cleverness of your analyzer, and it might take a long time. Some of these programs run for a long time. But if you combine such analysis with a set of rules that says how people could use it, you can actually see why the rules are violated, and that stops you from getting into the impossible com- uh, complexities. You don't want to solve the halting problem.

Lex Fridman 39:28 So static analysis is looking at the code without running the code.

Bjarne Stroustrup 39:32 Yes.

Lex Fridman 39:33 And thereby, it's almost not a production code, but it's almost like an educational tool of how the language should be used. It guides you. Like, at, at its best, right, it would guide you in how you write future code as well, and you learn together.

Bjarne Stroustrup 39:52 Yes. So basically, you need a set of rules for how you use the language. Then you need a static analysis that catches, um, your mistakes when you violate the rules or when your code ends up doing things that it shouldn't despite the rules because those are the language rules. We can go further. And again, it's back to my idea that I would much rather find errors before I start running the code. If nothing else, once the code runs, if it catches an error at runtimes, I have to have an error handler.

Lex Fridman 40:28 Hm.

Bjarne Stroustrup 40:28 And one of the hardest things to write in code is error-handling code because you know something went wrong. Do you know really exactly what went wrong? Usually not. How can you recover when you don't know what the problem was?

Lex Fridman 40:43 (laughs) Yeah.

Bjarne Stroustrup 40:44 You can't be 100% sure what the problem was in many, many cases. And s- this is, this is part of it. So yes, we, we, we, we need good languages with good type systems. We need rules for how to use them. We need static analysis, uh, and the ultimate for static analysis is, of course, program proof, but that still doesn't scale to the kind of systems we deploy. Then we start needing, uh, testing and uh, the rest of the stuff.

Lex Fridman 41:15 So

41:16 – 50:00

Zero-overhead principle in C++

Lex Fridman 41:16 C++ is an object-oriented programming language that creates, especially with its newer versions as we'll talk about, higher and higher levels of abstraction. So how do you design... Let's even go back to the origin of C++. How do you design something with so much abstraction that's still efficient and is still something that you can manage, do static analysis on, you can have constraints on, that can be reliable, all those things we've talked about? So create... The, the, to me, slightly... There's a slight tension between high-level abstraction and efficiency.

Bjarne Stroustrup 42:01 That's a good question. I could probably have a years course-

Lex Fridman 42:05 Yeah.

Bjarne Stroustrup 42:05 ... uh, just trying to answer it. Yes, there's a tension between efficiency and abstraction, but you also get the interesting situation that you get the best efficiency out of the best abstraction. And my main tool for efficiency, for performance, actually, is abstraction. So let's go back to how C++ got there.

Lex Fridman 42:30 Yeah.

Bjarne Stroustrup 42:32 You said it was an object-oriented programming language. I actually never said that.

Lex Fridman 42:37 (laughs)

Bjarne Stroustrup 42:37 It's always quoted, but I never did. I said, "C++ supports object-oriented programming-"

Lex Fridman 42:43 But it's not.

Bjarne Stroustrup 42:44 "... and other techniques."

Lex Fridman 42:45 Hm.

Bjarne Stroustrup 42:46 And that, that's important because I think that the best solution to most complex, interesting problems require-... ideas and take techniques from things that have been called object-oriented, um, data abstraction, functional, uh, traditional C-style code. All of the above.

Lex Fridman 43:13 Mm-hmm.

Bjarne Stroustrup 43:14 And so when I was designing C++, I soon realized I couldn't just add features. If you just add what looks pretty, or what people ask for, or what you think is good, one by one, you're not going to get a coherent whole. What you need is a set of guidelines that... that, that, that guide your decisions. Should this feature be in or should this feature be out? How should a feature be modified before it can go in and such? And there's a... In, in the book, I wrote about that, the design evolution of C++. There's a wh- a whole bunch of, of rules like that. Most of them are not language technical. They, they, they're things like don't violate static type system, because I like static type system for the obvious reason that I like things to be reliable on reasonable amounts of hardware. But one of these rules is, uh, is the zero overhead principle.

Lex Fridman 44:21 The what kind of pr-

Bjarne Stroustrup 44:22 The zero overhead principle.

Lex Fridman 44:23 Mm-hmm.

Bjarne Stroustrup 44:24 It basically says that if you have an abstraction, it should not cost anything compared to write the equivalent code at a lower level. So, if I have, say, a, a matrix multiply, it should be written in such a way that you could not, uh, drop to the C level of abstraction and use arrays and pointers and such and run faster. Um, and so people have written such, um, matrix multiplications and they've actually gotten code that ran faster than Fortran because-

Lex Fridman 45:07 Mm-hmm.

Bjarne Stroustrup 45:07 ... once you had the right abstraction, you can eliminate, um, you can eliminate temporaries and you can do, um, uh, loop fusion and other good stuff like that that's quite hard to do by hand and... in a lower level language. And there's some really nice examples of that. And the key here is that that matrix op- uh, multiplication, the matrix abstraction allows you to write code that's simple and easy. You can do that in any language. But with C++, it has the features so that you can also have this thing run faster than if you hand-coded it.

Lex Fridman 45:45 Mm-hmm.

Bjarne Stroustrup 45:45 Now, people have given that lecture many times, I and others. And a very common on- uh, question after the talk, where you have demonstrated that you can outperform Fortran for, uh, dense matrix multiplication, people come up and says, "Yeah, but that was C++. If I rewrote your code in C, how much faster would it run?" And the answer is much slower. This happened the first time actually back in the '80s with a friend of mine called Doug McIlroy who demonstrated the... exactly this effect. And, um, so the, the principle is you should give programmers the tools so that the abstractions can follow the zero overhead principle. Furthermore, when you put in a language feature in C++ or a standard library feature, you try to meet this. It doesn't mean it's absolutely optimal, but it means if you hand-code it with the usual th- uh, facilities in the language, in C++, in C, you should not be able to better it. Usually, you can do better if you use embedded Assembler for, uh, machine code for, for some of the details to utilize part of a computer that the compiler doesn't know about. But you should get to that point before you beat the abstraction.

Lex Fridman 47:09 So that's, that's a beautiful ideal to reach for. Uh-

Bjarne Stroustrup 47:13 And we meet it quite often.

Lex Fridman 47:14 Quite often. So where's the magic of that coming from? There's some of it is the com- compilation process, so the implementations in C++. Some of it is the design of the feature itself, the guidelines. So I've, uh, recently and often talked to Chris Lattner-

Bjarne Stroustrup 47:33 Mm-hmm.

Lex Fridman 47:33 ... so Clang. What, just out of curiosity, is your relationship in general with the different implementations in C++ as you think about you and committee and other people in C++ think about the design of new features or design of previous features, th- uh... W- uh, in, in trying to reach the ideal of zero overhead, w- does the magic come from the design, the guidelines or from the implementations?

Bjarne Stroustrup 48:06 And.

Lex Fridman 48:07 And?

Bjarne Stroustrup 48:08 Not or.

Lex Fridman 48:09 (laughs)

Bjarne Stroustrup 48:09 You ha- you, you are... You, you, you, you go for programming technique, program language features, and implementation techniques. You need all three.

Lex Fridman 48:19 And how can you think about all three at the same time?

Bjarne Stroustrup 48:23 It takes some experience, takes some practice, and sometimes you get it wrong. But after a while, you sort of get it right. I don't write compilers anymore, but Brian Kernighan pointed out that one of the reason C++ succeeded was some of the craftsmanship I put into the early compilers. And of course, I did the language design, and of course, I wrote a fair amount of code using this kind of stuff. And I think most of the successes involves progress in all three areas together.... a small group of people can do that. Two, three people can, can work together to do something like that. It's ideal if it's one person that has all the skills necessary, but nobody has all the skills necessary in all the fields where C++ is used. So if you want to approach my ideal in, say, concurrent programming, you need to know about algorithms from concurrent programming. You need to know the, the trickery of lock-free programming. You need to know something about, uh, compiler techniques. And then you have to know some of the program area- uh, the, uh, sorry, the application areas where this is, um, like, some forms of graphics or some forms of, um, uh, what do you call it? Um, a web server kind of stuff. And that's very hard to get into a single head. But small groups

50:00 – 54:46

Different implementation of C++

Bjarne Stroustrup 50:00 can do it too.

Lex Fridman 50:01 S- is- so is there differences in your view, not saying which is better or so on, but difference in the different implementations of C++? Why are there several? Sort of maybe a naive question for me. Um, GC- GCC, Clang, and so on.

Bjarne Stroustrup 50:21 You know, this is a very reasonable question. When I designed C++... Most languages have multiple implementations because if you run on an IPM, if you run on a Sun, if you run on a Motorola, there, there was just many, many companies and they each have their own compilation structure and their old compilers. It, it was just fairly common that there was many of them. And I wrote Cfront assuming that other people would write compilers for C++ if I was successful. And furthermore, I wanted to utilize all the backend infrastructures that were available. I soon realized that my users were using 25 different linkers. I couldn't write my own linker. Yes, I could, but I couldn't write 25 linkers and also get any work done on the language. And so, it came from a world where there was many linkers, many optimizers, many, uh, compiler frontends. Um, not to- not to start, um, but operat- many operating systems. The whole world was not an 86 and a, and a Linux box or something. Whatever is the standard today, in the old days, they said, uh, uh, set of backs. So basically, I assumed there would be lots of compilers. It was not a decision that there should be many compilers. It was just a fact. That's the way the world is.

Lex Fridman 51:55 Mm-hmm.

Bjarne Stroustrup 51:56 And yes, many compilers emerged. And today, there's at least four frontends, uh, Clang, GCC, Microsoft, and EDG, Eddie's Design Group. They, they supply a lot of the independent o- organizations and the embedded systems industry. And there's lots and lots of, of backends. We have to think about how many dozen backends there are. Uh, because different machines have different things. Especially in the n- m- embedded world, the machines are very different. The architectures are very different. And, um, so having a single implementation was never a, uh, an option. Now, I also happen to dislike monocultures.

Lex Fridman 52:53 Monocultures?

Bjarne Stroustrup 52:54 They are dangerous because whoever owns the mo- monoculture can go stale and, uh, there's no competition and there's no incentive to innovate.

Lex Fridman 53:06 Mm-hmm.

Bjarne Stroustrup 53:06 There's a lot of incentive to put barriers in the way of change because, "Hey, we own the world and, uh, it's a very comfortable world for us, and who are you to, um, to mess with that?" So, I really am very happy that there's four, uh, frontends for C++. Clang's great, but... GCC was great, but then it got somewhat stale. Clang came along and GCC is much better now.

Lex Fridman 53:37 (laughs) Competition is good.

Bjarne Stroustrup 53:38 Mi- Microsoft is much better now. So, a, a low... Uh, at least a low number of frontend puts a lot of pressure on standards compliance and also on performance and error messages and s- compile time speed. All this good stuff that we want.

Lex Fridman 54:00 (laughs) Do you think, crazy question, there might come along... Do you hope there might come along, uh, implementation of C++, written, given all of its history, written from scratch? So written today from scratch?

Bjarne Stroustrup 54:19 Well, Clang and the LLVM is more or less written by, uh, from scratch.

Lex Fridman 54:25 But there's been C++11, 14, 17, 20, you know? There's been a lot of feature-

Bjarne Stroustrup 54:31 I think sooner or later somebody's going to try again. There has been attempts, uh, to write new C++ compilers, and some of them has been used and some of them has been absorbed into others and such. Yeah, it'll happen.

Lex Fridman 54:45 So,

54:46 – 1:08:02

Key features of C++

Lex Fridman 54:46 what are the key features of C++? And let's use that as a way to sort of talk about the evolution of C++, the new features. So, at the highest level, what are the features that were there in the beginning? What features got added?

Bjarne Stroustrup 55:04 Let's first get a, a principle, uh, or an aim in place. Um, C++...... is for people who want to use hardware really well and then manage the complexity of doing that through abstraction.

Lex Fridman 55:21 Mm-hmm.

Bjarne Stroustrup 55:21 And so, the first facility you, you have is a way of manipulating the machines at a fairly low level. That looks very much like C. It has loops, it has variables, it has pointers with, like, machine addresses. It can access memory directly. It can allocate stuff in the absolute minimum of space needed on the machine. There's a machine-facing part of C++, which is roughly equivalent to C. I said C++ could beat C, and it can. It doesn't mean I dislike C. If I disliked C, I wouldn't have built on it. Furthermore, after Dennis Ritchie, I'm probably the major contributor to modern C. And, well, I had lunch with Dennis, uh, most days for 16 years, and we, we n- we never had a, a, a harsh word between us. So, uh, these C versus C++ fights are, are for people who don't quite understand what's going on. Uh, then the other part is the abstraction, and there, the key is the class, which is a user-defined type. And my idea for the class is that you should be able to build a type that's just like the built-in types in, in the way you use them, in the way you declare them, in the way you get the, the memory, and you can do just as well. So, in C++ there's an int, as in C. You should be able to build an abstraction, a class, which we can call capital int, that you could use exactly like an integer and run just as fast as an integer. There's the idea right there. And of course, you probably don't want to use the int itself, but it has happened. People have wanted integers that were range-checked so that you couldn't overflow and such.

Lex Fridman 57:28 Yeah.

Bjarne Stroustrup 57:28 Basically for very safety-critical, uh, applications, like the fuel injection for a marine diesel engine for the largest ships. This is a real example, by the way.

Lex Fridman 57:38 Mm-hmm.

Bjarne Stroustrup 57:39 Th- this has been done. They, they built themselves an integer that was just like integer except that it couldn't overflow. If there was an overflow, you went into the error-handling. Um, and then you built more interesting types. You, you, you can build a, a matrix, which you need to do graphics, or you could build a gnome for a, um, for a video game.

Lex Fridman 58:04 Mm-hmm. And all of these are classes and they appear just like the built-in types?

Bjarne Stroustrup 58:08 Exactly.

Lex Fridman 58:08 In terms of efficiency and so on. So, what else is there?

Bjarne Stroustrup 58:11 And flexibility.

Lex Fridman 58:13 So, uh, I don't know, for people who are not familiar with object-oriented programming, there's inheritance. There's a hierarchy of classes. You, you can, just like you said, create a generic vehicle-

Bjarne Stroustrup 58:27 Yep.

Lex Fridman 58:27 ... that can turn left.

Bjarne Stroustrup 58:29 So, what people found was that you don't actually know... How do I say this? A lot of types are related. That is, uh, the vehicles, all vehicles are related. Bicycles, cars, fire engines, tanks. They have some things in common and some things that differ and you would like to have the common things common and having the differences, uh, specific. And when you didn't want to know about the differences, like, just turn left.

Lex Fridman 59:08 Mm-hmm.

Bjarne Stroustrup 59:08 Um, you, you, you, you, you don't have to worry about it. That's how you get the traditional object-oriented programming coming out of SIMULA, adopted by Smalltalk and C++ and all, all the other languages. The other kind of obvious similarity between types comes when you have something like a vector.

Lex Fridman 59:28 Mm-hmm.

Bjarne Stroustrup 59:29 Fortran gave us the vector as called array of, um, doubles.

Lex Fridman 59:35 Mm-hmm.

Bjarne Stroustrup 59:35 But the minute you have a vector of doubles, you want a vector of double-precision doubles and for short doubles for graphics, and why should you have, not have a vector of integers while you're at it, or vet- vector of vectors and a vector of vectors of chess pieces? Now you have a board, right? So, this is... You express the re- uh, the commonality, uh, as, as the idea of a vector, and the variations come through parameterization! And so, here we get the two fundamental ways of abstracting, or of having similarities of types in C++. There's the inheritance and there's the parameterization. There's the object-oriented programming, and there's the generic programming.

Lex Fridman 1:00:28 Mm-hmm. With the templates for the generic programming.

Bjarne Stroustrup 1:00:30 Yep.

Lex Fridman 1:00:31 So, you, you've, uh, presented it very nicely, but now you have to make all that happen and make it efficient. So, generic programming with templates, there's all kinds of magic going on, especially r- recently, uh, that you can help catch up on. But it feels to me like you can do way more than what you just said with templates.

Bjarne Stroustrup 1:00:55 Mm-hmm. Mm-hmm.

Lex Fridman 1:00:55 You can start doing this kind of meta-programming, this kind of-

Bjarne Stroustrup 1:00:58 You can do meta-programming also. Uh, I, I didn't go there in, in that explanation. Uh, we're trying to be very basics. But go back onto the implementation.

Lex Fridman 1:01:08 Implementation.

Bjarne Stroustrup 1:01:08 If you couldn't implement this efficiently, if you couldn't use it...... so that it became efficient, it has no place in C++ because it will violate the zero-overhead principle.

Lex Fridman 1:01:21 Sure.

Bjarne Stroustrup 1:01:21 So, when I had to get object-oriented programming inheritance, I took the idea of virtual functions from Simula. Virtual functions is a Simula term, class is a Simula term. Uh, if you ever use those words, say thanks to, uh, Christian Nygaard and Ole Yvann Dahl. And I did the simplest implementation I knew of, which was basically a jump table. So, you get the virtual function table, the function goes in, uh, do- does an indirection through a table and get the right function. That's how you pick the right thing there. And, uh, I thought that was trivial. It's close to optimal. It's ... and it was obvious. It turned out that Simula had a more complicated way of doing it, and therefore was slower. And, um, it turns out that most languages have something that's a little bit more complicated, sometimes more flexible, but you pay for it.

Lex Fridman 1:02:21 Mm-hmm.

Bjarne Stroustrup 1:02:22 And one of the strengths of C++ was that you could actually do this object-oriented stuff and your overhead compared to- to ordinary functions, there's no indirections. It's sort of in 5%, 10%, 25%. Uh, just the call. It's- it's down there. It's not two.

Lex Fridman 1:02:42 Right.

Bjarne Stroustrup 1:02:42 And that means you can afford to use it. Furthermore, in C++, you have the distinction between a virtual function and a non-virtual function. If you don't want any overhead, if you don't need the indirection, uh, that gives you the flexibility in object-oriented programming, just don't ask for it.

Lex Fridman 1:03:01 (laughs)

Bjarne Stroustrup 1:03:02 Um, so the- the- the idea is that you only use virtual functions if you actually need the flexibility, so it's not zero overhead, but it's zero overhead compared to any other way of achieving the flexibility.

Lex Fridman 1:03:15 Mm-hmm.

Bjarne Stroustrup 1:03:16 Now, auto-parameterization. Basically, the compiler looks at, at the, the template, uh, say the vector, and it looks at the, um, parameter, and then combines the two and generates a piece of code that is exactly as if you had written a vector of that specific type.

Lex Fridman 1:03:44 Yes.

Bjarne Stroustrup 1:03:44 So that's the- that- that's the minimal overhead. If you have many template parameters, you can actually combine code that the compiler couldn't usually see at the same time, and therefore get code that is faster than if you had handwritten the stuff, uh, unless you are very, very clever.

Lex Fridman 1:04:05 So, the thing is, parameterized code, the compiler fills stuff in during the compilation process, not during runtime.

Bjarne Stroustrup 1:04:14 That's right.

Lex Fridman 1:04:16 So-

Bjarne Stroustrup 1:04:16 And furthermore, it gives all the information it's gotten, which is the template, the parameter, and the context of use. It combines the three and generates good code.

Lex Fridman 1:04:30 Yeah. But it can generate ... now, it's a little outside of what I'm even comfortable thinking about, but it can generate a lot of code.

Bjarne Stroustrup 1:04:40 Yes.

Lex Fridman 1:04:41 And how do you ... now, I remember being both amazed at the power of that idea and how ugly the debugging looked. (laughs)

Bjarne Stroustrup 1:04:53 Yes, uh, debugging can be truly horrid. Come back to this because I have a solution.

Lex Fridman 1:04:59 Okay. (laughs)

Bjarne Stroustrup 1:04:59 Um, anyway, the debugging was ugly. The code generated by C++ has always been ugly because there's these inherent optimizations. A modern C++ compiler has front-end, middle-end, and back-end optimizations. Even Cfront back in '83 had front-end and back-end optimizations. I actually took the code, generated an internal representation, munched that implement- uh, uh, representation to generate good code.

Lex Fridman 1:05:34 Mm-hmm.

Bjarne Stroustrup 1:05:34 So people says, "It's not a compiler, it generates C." The reason it generated C was I wanted to use the C's code generators that were really good at back-end optimizations, but I needed front-end optimizations, and therefore the C I generated was optimized C.

Lex Fridman 1:05:50 Mm-hmm.

Bjarne Stroustrup 1:05:51 The way a, um, a really good opt- handcrafted optimizer human could, could generate it, and it was not meant for humans. It was the output of a program, and it's much worse today. And with templates, it gets much worse still. So it's hard to d- uh, it's hard to combine simple debugging with simple ... with- with the optimal code, because the idea is to drag in information from different parts of the code to generate good code, uh, uh, machine code. And that's not readable. So, what people often do for debugging is they turn the optimizer off.

Lex Fridman 1:06:36 Mm-hmm.

Bjarne Stroustrup 1:06:37 And so you get code that when you- when- when something in your source code looks like a function call, it is a function call. When the optimizer is turned on, it may disappear, the function call, it may inline, and so one of the things you can do is you can actually get code that is smaller than the function call because you eliminate the function preamble and, uh, return and, uh, there's just the operation there. One of the key things when I did, uh, templates was I wanted to make sure that if you have a, say, sort algorithm and you give it a-... sorting criteria. If that sorting criteria is simply comparing things with lesson, the code generator should be the less than, not a f- indirect function call to a, a, a comparison, uh, object, which is what it is in the source code. But we really want down to the single instruction. And, um, but anyway, turn off the optimizer, and, and-

Lex Fridman 1:07:52 You can get into bugs.

Bjarne Stroustrup 1:07:54 ... you can debug. The first level of debugging, uh, can be done, and I always do without the optimization on, because then I can see what's going on.

1:08:02 – 1:18:06

C++ Concepts

Lex Fridman 1:08:02 And then there's this idea of concepts-

Bjarne Stroustrup 1:08:05 Yes.

Lex Fridman 1:08:05 ... that puts some ... Now, I've never even ... J- I don't know if it was ever available in any form, but, uh, it puts some constraints on the stuff you can parametrize, essentially?

Bjarne Stroustrup 1:08:20 Uh, let me try and explain.

Lex Fridman 1:08:21 Yes .

Bjarne Stroustrup 1:08:22 Um, so yes, it wasn't there 10 years ago. We have had versions of it that actually work for the last four, five years. Um, it was a design by Gaby Dos Reis, uh, Andrew Soddon, and me. We were professors and post-docs in Texas at the time. And, um, the implementation by Andrew Soddon has been available for, uh, that time, and it is part of C++20. Uh, and there's a standard library that uses it. So, this is becoming really very real. It's available in, uh, Clang and, and GCC. Uh, GCC for a couple of years, and I believe Microsoft is soon, soon going to do it. We expect all of C++20 to be available, so in all the major compilers in '20. But this kind of stuff-

Lex Fridman 1:09:27 Can y-

Bjarne Stroustrup 1:09:27 ... is, is available now.

Lex Fridman 1:09:29 Yeah.

Bjarne Stroustrup 1:09:29 I'm just saying that because otherwise people might think I was talking about science fiction. And so what I'm going to say-

Lex Fridman 1:09:36 This is real.

Bjarne Stroustrup 1:09:36 ... is concrete. You can run it today, and there's production users of it. So, the basic idea is that when you have a, a, a generic component, uh, like a sort function, the sort function will, will require at least two parameters. One, uh, a data structure with a given type, and a comparison criteria. And these things are related, but, um, obviously you can't compare things if you don't know what the type of the things you compare. And so, you want to be able to say, "I'm going to sort something, and it is to be sortable." What does it mean to be sortable? You look it up in the standard. It has to have ... It has to be a sequence with a begin and an end.

Lex Fridman 1:10:27 Mm-hmm.

Bjarne Stroustrup 1:10:28 There has to be random access to that sequence, and there has to be ... Um, the, the element types has to be comparable. By default, by less than.

Lex Fridman 1:10:39 Which means less than operator can operate on them.

Bjarne Stroustrup 1:10:41 By ... By ... Yes.

Lex Fridman 1:10:41 Lots of logical operators can operate on them.

Bjarne Stroustrup 1:10:42 So, basically, what concepts are, they're compile time predicates. They're predicates you can ask, "Are you a sequence?"

Lex Fridman 1:10:51 Mm-hmm.

Bjarne Stroustrup 1:10:51 "Yes, I have a begin and end." "Uh, are you a random exit sequence?" "Yes, I have, uh, subscripting and plus." "Uh, is your element type something that has a less than?" "Yes, I have a less than. It's &." So basically, that's the system. And so instead of saying, "I will take a parameter of any type," it'll say, "I'll take something that's sortable and it's well-defined." And so we say, "Okay, um, you can sort with less than. I don't want less than. I want greater than, or s- or something I invent." So, you have two parameters, the sortable thing and the, uh, comparison criteria. And the comparison criteria will say, "Well, I can, um ... You, you can write it saying it, it should operate on the element type, and it has the comparison operations." So, that's th- simply the fundamental thing. It's compile time predicates. Do you have the properties I need?

Lex Fridman 1:11:52 Mm-hmm.

Bjarne Stroustrup 1:11:52 So, it specifies the requirements of the code on the parameters that it gets.

Lex Fridman 1:11:59 At compile time.

Bjarne Stroustrup 1:11:59 It's very similar to types, actually.

Lex Fridman 1:12:03 But operating in the space of concepts. (laughs) So-

Bjarne Stroustrup 1:12:07 Yeah, concepts. Uh, the, the word concept was, uh, used by Alex Stefanov, who is sort of the father of generic programming in the context of C++. You know, there's other pla- uh, places that use that word, but the way we call generic programming is Alex's. And he called them concepts because he said they're, they're the sort of the fundamental concepts of an area, so they should be called concepts.

Lex Fridman 1:12:34 (laughs)

Bjarne Stroustrup 1:12:34 And we've had concepts all the time. If you look at the K&R book about C, C has arithmetic types and it has, um, um, integral types. It says so in the book. And then it lists what they are, and they have certain properties. The difference today is that we can actually write a concept that will ask a type, "Are you an integral type?"

Lex Fridman 1:13:01 Mm-hmm.

Bjarne Stroustrup 1:13:01 "Y- Do you have the properties necessary to be an integral type? Do you have plus, minus, divide, and such?"

Lex Fridman 1:13:08 So, uh, maybe the story of concepts... Because I thought it might be part of C++11.

Bjarne Stroustrup 1:13:18 Sorry to-

Lex Fridman 1:13:18 Uh, C, C, COX, whatever-

Bjarne Stroustrup 1:13:21 Yeah.

Lex Fridman 1:13:21 ... it was at the time.What, what was the... why didn't it j- what, like what... we'll, we'll talk a little bit about this fascinating process of standards, 'cause I, I think it's really interesting for people, it's interesting for me. But why did it take so long? What shapes did the idea of concepts take? What were the challenges?

Bjarne Stroustrup 1:13:43 Back in '87 or thereabouts, I was tr-

Lex Fridman 1:13:47 1987? Wow.

Bjarne Stroustrup 1:13:49 1987 or thereabouts, when I was designing templates, obviously I wanted to express the notion of what is required by a template of, of its arguments, and so I looked at this. And basically for, for templates, I wanted three properties. I want it to be very flexible. It had to be able to express things I couldn't imagine, because I know I can't imagine everything and I've been suffering from languages that try to constrain you to only do what your, the designer thought good.

Lex Fridman 1:14:26 Mm-hmm.

Bjarne Stroustrup 1:14:27 Didn't want to do that. Secondly, it had to run faster, as fast or faster than handwritten code. So basically if I have a vector of T and I take a vector of char, it should run as fast as you built a vector of char yourself without parameterization. And second... and thirdly, I wanted to be able to express the constraints of, of the arguments, have proper type checking of the interfaces. And neither I nor anybody else at the time knew how to get all three. And I thought, for C++, I must have the two first. Otherwise it's not C++. And it bothered me for another couple of decades that I couldn't solve the third one. I mean, I was the one that put function argument type checking into C. I know the value of good interfaces. I didn't invent that idea, it's very common, but I did it, and I wanted to do the same for templates of course, and I couldn't. So it bothered me. Then we tried again 2002, 2003, 2003. Uh, Gabby Dres and I started analyzing the problem, uh, explained possible solutions. There was not a complete design. Uh, a group in University of Indiana, an old friend of mine, uh, they started a, a project at Indiana and we, we thought we could get a good system of concepts in another two or three years, that would have made C++ ele- uh, 11 to C++. Well, it, it turned out that I think we got a lot of the fundamental, uh, ideas wrong. They were too con- uh, conventional. Um, they didn't quite fit C++ in my opinion. Didn't serve implicit conversions very well. It didn't serve mixed, mixed type arithmetic, mixed type competation, uh, computations very well. A lot of stuff came out of the functional community and it... that community didn't deal with multiple types in, in the same way as C++ does, had more constraints on, on what you could express and didn't have the draconian, uh, performance requirements. And basically we tried, we tried very hard. We had some successes, but it just in the end wasn't... (sighs) didn't compile fast enough, was too hard to use and, uh, didn't run fast enough unless you had s- uh, optimizers that was beyond the, uh, state of the art. They still are. So we had to do something else. Basically it was the idea that a set of parameters has, uh, defines a set of operations and you go through an indirection table just like for virtual functions, and then you try to optimize the, um, indirection away, uh, to get performance.

Lex Fridman 1:18:02 Mm-hmm.

Bjarne Stroustrup 1:18:02 And we just couldn't do all of that. Uh,

1:18:06 – 1:28:05

C++ Standards Process

Bjarne Stroustrup 1:18:06 but get back to the standardization. We are standardizing C++ under ISO rules, which are very open process. People come in, there's no requirements for education or experience.

Lex Fridman 1:18:21 So you've started to develop C++ and there's a hope w- when was the first standard established? What is that like? Uh, the ISO standard, is there a committee that you're referring to?

Bjarne Stroustrup 1:18:34 Sure.

Lex Fridman 1:18:34 There's a group of people. What i- what's that like? How often do you meet?

Bjarne Stroustrup 1:18:39 Okay.

Lex Fridman 1:18:39 What's the discussion like?

Bjarne Stroustrup 1:18:39 I'll, I'll try and explain that. So sometime in early 1989, um, two people, one from IBM, one from HP turned up in my office and told me, "I would like to standardize C++." This was a new idea to me and, uh, I pointed out that w- it wasn't finished yet and it wasn't ready for formal standardization and such. And they said, "No, Bjarne, you haven't gotten it. You, you really want to do this. Um, our organizations depend on C++. We cannot, uh, depend on something that's owned by another corporation that might be a competitor. Of course, we could rely on you."... but you might get run over by a bus.

Lex Fridman 1:19:32 Right, the old, the old bus, right?

Bjarne Stroustrup 1:19:33 "We really need to get this out in the open. It has to be, uh, standardized under formal rules, and, uh, we w- are going to standardize it, um, under ISO rules, and you really want to be part of it because basically, otherwise, we'll do it ourselves. And we know you can do it better." Uh, so, uh, through a, a combination of arm-twisting and, uh, flattery, um-

Lex Fridman 1:20:04 A carrot and stick.

Bjarne Stroustrup 1:20:05 ... it got started.

Lex Fridman 1:20:05 Yep.

Bjarne Stroustrup 1:20:05 So in late, in late '89, there was a meeting in DC at the, um... actually, no. It was not ISO then, it was ANSI, the American National Standard we're doing. We met there, we were lectured on the rules of how to do an ANSI standard. There was about 25 of us there, which apparently was a new record for that kind of meeting. Um, and, um, some of the old C guys that has been standardizing C was there, so we got some expertise in. So, the way this works is that it's an open process. Anybody can, can sign up if they pay the minimal fee, which is just about a thousand dollars. It was less then, it's a little bit more now. And, uh, I think it's $1,280.

Lex Fridman 1:20:59 (laughs)

Bjarne Stroustrup 1:20:59 It's not, it's not going to kill you.

Lex Fridman 1:21:01 Right.

Bjarne Stroustrup 1:21:01 And y- we have three meetings a year, is, is fairly standard. Uh, we tried two meetings a year for a couple of years, that didn't work too well. So three meet- uh, three one-week meetings a year, and you meet and you have technical d- m- meet, uh, technical discussions, and then you bring proposals forward for votes. The votes are done one person per... one vote per organization. So you can't have, uh, say, IBM come in with 10 people and, uh, dominate things. That's not allowed.

Lex Fridman 1:21:42 And these are organizations that extensively use C++? 'Cause-

Bjarne Stroustrup 1:21:46 Yes.

Lex Fridman 1:21:47 'Cause, 'cause it's basically-

Bjarne Stroustrup 1:21:47 Or, or individuals.

Lex Fridman 1:21:48 Or individuals. I mean, it's, uh, it's a, it's a bunch of people in a room deciding the design of a language based on which a lot of the world's systems run.

Bjarne Stroustrup 1:22:00 That's right. Well, hmm, I think most people would agree it's better than if I decided it, or better than if a single organization like AT&T decides it.

Lex Fridman 1:22:12 I don't know if everyone agrees to that, by the way. Bureaucracies have their critics too.

Bjarne Stroustrup 1:22:17 Yes. They, they're there... Look, standardization is not pleasant.

Lex Fridman 1:22:23 Yeah.

Bjarne Stroustrup 1:22:23 It's, it's, it's, it's, it's horrifying.

Lex Fridman 1:22:25 It's like democracy, right?

Bjarne Stroustrup 1:22:26 But we... exactly. Uh, as Churchill says, "Democracy is the worst way except for all the others," right? And it's, I would say, the same with formal standardization. But anyway, so we meet and we, uh, we, we have these votes, and that determines what the standard is. Couple of years later, we extended this, so it became worldwide. We have standard organizations that are active in, currently, 15 to 20 countries, and, uh, another 15 to 20 are, are sort of looking and then voting based on the rest of the work on it. A- and we meet three times a year. Uh, next week, I'll be in Cologne, Germany, uh, spending a week, uh, doing standardization, and we'll vote out the committee draft, or C++20, which goes to the national, uh, standards committees for comments and requests for changes and improvements. Then we do that, and there's a second, uh, set of votes where hopefully everybody votes in favor. This has happened several times. The first time we finished, we started in... the first technical meeting was in 1990. The last was in '98. We voted it out. That was the standard that people used till 11, or a little bit past 11, um, and it was an international standard. All the countries voted in favor. Um, it took longer with 11. I'm not gonna mention why, but all the nations voted in favor. Uh, and we work on the basis of, uh, consensus. That is, we do not want something that passes 60/40, uh, because then we're going to get dialects and opponents and people complain too much. Eh, they all complain too much, but, eh, basically-

Lex Fridman 1:24:33 Eventually, you won.

Bjarne Stroustrup 1:24:33 ... it has no real effect. The, the standards has been obeyed. They have been working to make it, uh, easier to use many compilers, many computers, and all of that kind of stuff. And so the first... The, the, the... it was traditional with ISO standards to take 10 years. We did the first one in eight brilliant.

Lex Fridman 1:24:55 (laughs)

Bjarne Stroustrup 1:24:56 Uh, and we thought we were going to do the next one in six because now we are good at it.

Lex Fridman 1:25:00 Right.

Bjarne Stroustrup 1:25:01 It took '13.

Lex Fridman 1:25:04 Yeah. It was, uh, named 0x.

Bjarne Stroustrup 1:25:07 It was named 0x.

Lex Fridman 1:25:08 Hoping, uh, that you would at least get it in, within the single, within the aughts, the single digits.

Bjarne Stroustrup 1:25:13 I, I thought we would get, uh, I thought we would get to six, seven, or eight.

Lex Fridman 1:25:17 The confidence of youth.

Bjarne Stroustrup 1:25:19 Yeah, that's right. Well, the point is that this was sort of like a second, um, system effect. That is, we now knew how to do it, and so we're going to do it much better, and we got more ambitious-

Lex Fridman 1:25:31 Ambitious created 10.

Bjarne Stroustrup 1:25:31 ... and it took longer. Furthermore, there is this tendency because it's a 10-year cycle-

Lex Fridman 1:25:38 Mm-hmm.

Bjarne Stroustrup 1:25:39 ... or eight, doesn't matter. Just before you're about to ship, somebody has a bright idea.

Lex Fridman 1:25:47 (laughs) Yeah.

Bjarne Stroustrup 1:25:49 And so we really, really must get that in. We, we did that successfully with the STL.

Lex Fridman 1:25:59 Yeah.

Bjarne Stroustrup 1:25:59 We got the, the, the standard library-

Lex Fridman 1:26:02 Standard template library.

Bjarne Stroustrup 1:26:02 ... that gives us all the STL stuff. That, that, uh, basically I think it saved C++. It was beautiful.

Lex Fridman 1:26:08 Yeah, it was, and is a beautiful thing.

Bjarne Stroustrup 1:26:09 And then people tried it with other things and it didn't work so well. They got things in, but it wasn't as, as dramatic and it took longer and longer and longer. So after C++11, which was a huge improvement, and, uh, what, basically what most people are using today, we decided never again. And so how do you avoid those slips? And the answer is that you ship more often so that if you, if you, if you have a slip on a 10-year cycle, by the time, you know, it's a slip, there's 11 years till you get it.

Lex Fridman 1:26:50 Yeah.

Bjarne Stroustrup 1:26:50 Now with a three-year cycle, there is about three, uh, four years till you get it, like, uh, the, the delay between feature freeze and, uh, shipping, so we always get one or two years more.

Lex Fridman 1:27:05 Yeah.

Bjarne Stroustrup 1:27:05 And so we, we shipped 14 on time, we shipped 17 on time, and we ship, we will ship 20 on time. It's, it, it'll happen. And furthermore, this allow... This gives a predictability that allows the implementers, the compiler implementers, the library implementers to... They have a target and they deliver on it. 11 took two years before, uh, most compilers were good enough. 14, most compilers were actually getting pretty good in 14. 17, everybody shipped in 17.

Lex Fridman 1:27:46 Wow.

Bjarne Stroustrup 1:27:46 We are going to have at least almost everybody ship almost everything in 20. And I know this because they're shipping in 19.

Lex Fridman 1:27:56 (laughs)

Bjarne Stroustrup 1:27:57 Predictability is good. Delivery on time is good. And so, yeah.

Lex Fridman 1:28:02 That's great. So-

Bjarne Stroustrup 1:28:03 That's how it works.

1:28:05 – 1:31:52

Constructors and destructors

Lex Fridman 1:28:06 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, just to you personally, beautiful or just you sit back and think, "Wow, that's just nice and clean feature of C++"?

Bjarne Stroustrup 1:28:37 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, uh, 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 the, of a type, for an object, and the destructor that cleans up any messes at the end of it. That is the key to C++. That's why we don't have to use garbage collection. That's how we can get predictable perf- performance. That's how you can get, um, 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 RII, um, RAII, resource acquisition is initialization, which is the idea that you grab resources in the constructor and release them in destructor. Um, it's also the best example of why I shouldn't be in advertising.

Lex Fridman 1:29:52 (laughs)

Bjarne Stroustrup 1:29:52 I get the best idea and I call it resource acquisition is initialization. Um, not the greatest naming I've ever heard.

Lex Fridman 1:30:02 (laughs) Uh, so it's types, abstraction of types, uh, you said, "I want to create my own types." So types is an essential part of C++-

Bjarne Stroustrup 1:30:16 Yes.

Lex Fridman 1:30:16 ... and making them efficient is the ef- is the key part. And to you the... 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. Is, uh, that's almost like-

Bjarne Stroustrup 1:30:43 Um, a, a little bit-

Lex Fridman 1:30:43 ... birth and death is what defines human life. (laughs)

Bjarne Stroustrup 1:30:46 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-

Lex Fridman 1:31:06 Hmm.

Bjarne Stroustrup 1:31:07 ... in about the second week of my work with what was then called civil classes. It is a fundamental idea. Uh, next comes the fact that you need to control copying, because once you control, as you said, birth and death-

Lex Fridman 1:31:22 Mm-hmm.

Bjarne Stroustrup 1:31:22 ... you have to control taking copies, uh, which is another way of creating an object. And finally, you have to be able to move things around.

Lex Fridman 1:31:32 Mm-hmm.

Bjarne Stroustrup 1:31:33 So you get the move operations, and that's the set of key operations you can define on a C++ type.

Lex Fridman 1:31:42 And so to you, c-... those things are just a beautiful part of C++, that is at the core of it all.

Bjarne Stroustrup 1:31:51 Yes.

1:31:52 – 1:38:10

Unified theory of programming

Lex Fridman 1:31:53 You mentioned that you hope there will be one unified set of guidelines in the future for how to construct a programming language. So perhaps not one programming language, but a unification of how we build programming languages, if you remember such statements.

Bjarne Stroustrup 1:32:10 Yeah. I, I have some trouble remembering it, but I know the origin of that idea.

Lex Fridman 1:32:15 So, maybe you can talk about ... So C++ has been improving. There's been a lot of programming language. Do you ... Where does the arc of history taking us? Do you hope that there is a unification about the languages with which we communicate in the digital space?

Bjarne Stroustrup 1:32:32 Well, I, I think that languages should be designed not by, uh, clobbering language features together and doing slightly different versions of somebody else's ideas, but through the creation of a set of, um, principles, rules of thumbs, whatever you call them. Um, I, I made them for C++. And we're trying to teach people in the standards committee about these rules, because a lot of people come in and says, "I've got a great idea, let's put it in language." And then you have to ask, "Why does it fit in the language? Why does it fit in this language? It may fit in another language and not here, or it may fit here and not the other language." So, you have to work from a set of principles and you have to develop that set of principles. And sp- ... One example that, that I sometimes remember is I was sitting down with some of the designers of Common Lisp.

Lex Fridman 1:33:40 Mm-hmm.

Bjarne Stroustrup 1:33:41 And we were talking about languages and language features, and obviously we didn't agree about anything, uh, because, well, Lisp is not C++ and vice versa.

Lex Fridman 1:33:53 There's too many parentheses.

Bjarne Stroustrup 1:33:55 But suddenly we started making progress. I said, "I had this problem and I developed it according to these ideas." And they said, "What? Why? We had that problem, different problem, and we developed it with the same kind of principles." And so we worked through large chunks of C++ and large chunks of Common Lisp, and figured out we actually had similar sets of principles of how to do it. But the constraints on our designs were very different, and the aims for the usage was very different. But there, there, there was commonality I- in the way you reason about language features and the fundamental principles you're trying to do.

Lex Fridman 1:34:46 So do you think that's possible to ar- ... So there ... Just like there is perhaps a unified theory of physics, of the fundamental forces of physics, (laughs) I'm sure there is, uh, commonalities among the languages, but there's also people involved-

Bjarne Stroustrup 1:35:05 Yeah.

Lex Fridman 1:35:05 ... that help drive the development of these languages. Do you have a hope or an optimism that there will be a unification if you th- if you think about physics and Einstein towards a simplified language?

Bjarne Stroustrup 1:35:21 Uh, I-

Lex Fridman 1:35:21 Do you think that's possible?

Bjarne Stroustrup 1:35:24 Let's remember, sort of modern physics, I think started with Galileo in the 1300s. So, they've had, uh, 700 years to get going.

Lex Fridman 1:35:35 Right.

Bjarne Stroustrup 1:35:35 Modern computing started in about '49. Hmm, we've got, what is that? Uh, 70 years. They have 10, 10 times.

Lex Fridman 1:35:46 Yeah.

Bjarne Stroustrup 1:35:46 And furthermore, they, they, they're not as bothered with people using physics, uh, the way we are worried about programming is done by humans.

Lex Fridman 1:35:57 Sure.

Bjarne Stroustrup 1:35:57 So, uh, each have, um, problems and constraints the others have, but we are very immature compared to physics. Um, so I would look at sort of the philosophical level a- and, and look for fundamental principles. Like you don't leak resources you shouldn't. Uh, you don't take errors at runtime that you don't need to. Um, you don't violate some kind of type system. There's many kinds of type systems, but when you have one, you don't break it, um, et cetera, et cetera. Uh, there, there will be quite a few and it'll not be, uh, be the same for all languages. But I think we ... If we step back at some kind of philosophical level, we can ... We would be able to agree on sets of principles that applied to, to, to sets of problem areas. And within an area of use, like in c++'s case, uh, what used to be called systems programming, the area between the hardware and the, the, the, the fluffier parts of the system, you, you might very well see a convergence. So these days you see Rust having a, uh, adopted RAII and sometime accuses me for having borrowed it 20 years before they discovered it.

Lex Fridman 1:37:27 (laughs)

Bjarne Stroustrup 1:37:27 But, um, it's, uh, uh, we're, we're seeing some kind of conversion, uh, con- convergence here instead of, um, relying on garbage collection all the time. The garbage collection languages are, are doing things like the, uh ...... uh, dispose patterns and such that imitates, uh, some of the, uh, construction/destruction stuff, and they're trying not to use the garbage collection all the time and things like that. So, there's- there- there's conversion, but I think we have to step back to the philosophical level, agree on principles, and then we'll see some conversions co- con- convergences, and it will be application domain

1:38:10 – 1:44:20

Machine learning

Bjarne Stroustrup 1:38:10 specific.

Lex Fridman 1:38:12 So, a crazy question, but I work a lot with machine learning, with deep learning. I'm not sure if you touch that world m- much, but y- you could think of programming as a thing that takes some input... Uh, uh, programming is the task of creating a program, and the program takes some input and produces some output. So, machine learning systems train on data in order to be able to take an input and produce output, but they're messy, fuzzy things. Much like, uh, we as children grow up, you know, we take some input, we take, make some output, but we're noisy, we mess up a lot, we're definitely not reliable. Biological system are a giant mess. So, there's a sense in which machine learning is a kind of way of programming, but just fuzzy.

Bjarne Stroustrup 1:39:04 Mm-hmm.

Lex Fridman 1:39:05 It's very, very, very different (laughs) than C++. 'Cause C++ is a, like, it's just like you said, it's extremely reliable, it's efficient, it's, uh, you know, you can- you can measure, you can test it in a bunch of different ways. With biological systems, or machine learning systems, you can't say much except sort of empirically saying that 99.8% of the time, it seems to work. Uh, what do you think about this fuzzy kind of programming? I- and- and do you- do you even see it as programming? Is it solely and totally another kind of world?

Bjarne Stroustrup 1:39:43 Mm-hmm. I- I think it's a different kind of world, uh, and it is fuzzy, and in my domain, I don't like fuzziness.

Lex Fridman 1:39:50 (laughs)

Bjarne Stroustrup 1:39:51 That is, people say things like they want everybody to be able to program, but I don't want everybody to program my, um, my- my- my- my aeroplane controls or the car controls. Uh, I want that to be done by engineers. I want that to- to be done with people that are specifically educated and trained for doing, uh, building things, and it is not for everybody. Similarly, a language like C++ is not for everybody. It is generated to be a sharp and effective tool for, uh, professionals basically, uh, and definitely for people who- who- who aim at some kind of precision. You don't have people doing calculations without understanding math.

Lex Fridman 1:40:45 Mm-hmm.

Bjarne Stroustrup 1:40:46 Right? Uh, counting on your fingers is not going to cut it if you want to fly to the moon. And, um, so there are areas where an 84% accuracy rate, uh, 16%, um, false positive rate is perfectly acceptable and where people will probably get no more than 70. You said 98%, I- m- th- what I have seen is more like 84-

Lex Fridman 1:41:16 (laughs)

Bjarne Stroustrup 1:41:16 ... and by- by really a lot of blood, sweat, and tears, you can get up to 92 and a half.

Lex Fridman 1:41:22 Right.

Bjarne Stroustrup 1:41:22 Uh, so this is fine if it is, say, pre-screening, uh, stuff before the human look at it. It is not, uh, good enough for- for life-threatening situations and, um, so there's lots of areas where- where the fuzziness is- is perfectly acceptable and good and better than humans, cheaper than humans, but it's not the kind of engineering stuff I'm mostly interested in. I- I worry a bit about machine learning in the context of cars. You know much more about this than I do.

Lex Fridman 1:42:00 I worry too.

Bjarne Stroustrup 1:42:01 But I'm- I- I'm- I'm- sort of a- an amateur here. I've read some of the papers, but I have not ever done it. And the- the- the- the idea that scares me the most is the one I have heard, and I don't know how common it is, that you have this AI system, machine learning, all of these trained, uh, neural nets, a- and when, uh, something is too complicated, they ask the human for help. But the human is reading a book or asleep, and he has 30 seconds or three seconds to figure out what the problem was that the AI system couldn't handle and do the right thing. This is scary. I mean, how do you do the cut over between the machine and the human?

Lex Fridman 1:42:58 It's very, very difficult and for the designer of a- one of the most reliable, efficient, and powerful programming languages, C++, I can understand why that world is actually unappealing. It is for most engineers. To me, it's extremely appealing because we don't know how to get that interaction right, but I think it's possible, but it's very, very hard.

Bjarne Stroustrup 1:43:25 It is.

Lex Fridman 1:43:26 And-

Bjarne Stroustrup 1:43:26 I mean, I was, uh, stating a problem, not a- a solution.

Lex Fridman 1:43:28 Yes, that it's impossible.

Bjarne Stroustrup 1:43:30 I mean-

Lex Fridman 1:43:31 I would much rather never rely on a human. If you're driving a nuclear reactor, if you're... Or an autonomous vehicle, it would- it's much better to design systems written in C++ that never ask human for help.

Bjarne Stroustrup 1:43:44 Le- let- let- let's just get one fact in.

Lex Fridman 1:43:47 Yeah.

Bjarne Stroustrup 1:43:48 ... all of this AI stuff is on top of C++. (laughs)

Lex Fridman 1:43:51 (laughs) Nice.

Bjarne Stroustrup 1:43:53 So, uh, so tha- that's one reason I have to keep a weather eye out on what's going on in that field.

Lex Fridman 1:43:59 (laughs)

Bjarne Stroustrup 1:44:00 But I will never become an expert in that area. But it's a good example of how you separate different areas of applications and you have to have different tools, different principles, um, and, and they interact. No major system today is written in one language, and there are good reasons for that.

1:44:20 – 1:45:29

Proudest moment

Lex Fridman 1:44:21 When you look back at your life work, what is a, what is a moment, what is a event, creation, that you're really proud of, that you say, "Damn, I, I did pretty good there?" Is it as obvious as the creation of C++?

Bjarne Stroustrup 1:44:41 It's obvious. I've spent a lot of time with C++, and it's a combination of a few good ideas, a lot of hard work, and a bit of luck. And I've tried to get away from it a few times, but I get dragged in again, partly because I'm most effective in this area and partly because what I do, uh, has much more impact if I do it in the context of, of C++. I, I have four and a half million people that pick it up, uh, tomorrow if I get something right. If I did it in another field, I would have to start learning, then I have to build it, and then I'll see if anybody wants to use it. One of the things that has kept me going for all of these years is, one, the good things that people do

The published transcript ends here. Continue watching on YouTube

Episode duration: 1:47:12

In the uListen app, the transcript lights up word by word as you listen. Get uListen

Transcript of episode uTxRF5ag27A

Get more out of YouTube videos.

Read along on your phone, search it beside the video, or ask your favorite AI app what was said.