← Lex Fridman

Bjarne Stroustrup: C++

第2019-11-07期2019-11-07English

嘉宾:Bjarne Stroustrup

收听本集00:00 / 1:47:19
阅读设置
按你的习惯阅读
正文字号
下载播客精读
把这一篇,带走慢慢读

精读待补充

下载功能还未开放

§01Transcript · 00:00–16:05

00:00The following is a conversation with Bjorn Stroustrup.

00:03He's the creator of C++, a programming language that after 40 years

00:08is still one of the most popular and powerful languages in the world.

00:12Its focus on fast, stable, robust code underlies many of the biggest systems in

00:17the world that we have come to rely on as a society.

00:20If you're watching this on YouTube, for example, many of the critical back-end

00:24components of YouTube are written in C++. Same goes for

00:28Google, Facebook, Amazon, Twitter, most Microsoft

00:32applications, Adobe applications, most database systems, and most physical

00:37systems that operate in the real world, like cars, robots, rockets that launch us

00:42into space, and one day will land us on Mars.

00:46C++ also happens to be the language that I use more than any other in my life.

00:52I've written several hundred thousand lines of C++ source code.

00:56Of course, lines of source code don't mean much, but they do give hints of my

01:01personal journey through the world of software. I've enjoyed watching the

01:05development of C++ as a programming language,

01:08leading up to the big update in a standard in 2011,

01:12and those that followed in '14, '17, and toward the new C++20

01:18standard, hopefully coming out next year. This is

01:21the Artificial Intelligence Podcast. If you enjoy it,

01:24subscribe on YouTube, give it five stars on iTunes,

01:28support it on Patreon, or simply connect with me on Twitter

01:31at Lex Friedman, spelled F-R-I-D-M-A-N. And now, here's my conversation with Bjorn

01:38Stroustrup. What was the first program you've ever

01:42written? Do you remember? It was my second year in

01:46university, first year of computer science, and it

01:51was in Alcol 60. I calculated the shape of a

01:56super ellipse and then connected points on the perimeter, creating star

02:03patterns. It was with a wet ink

02:08on a paper printer. And that was in college?

02:12Yeah, I learned to program the second year in university.

02:18And what was the first programming language,

02:21if I may ask it this way, that you fell in love with?

02:26I think Alcol 60, and after that I remember

02:34Snowball, I remember Fortran, didn't fall in love with that, I remember

02:40Pascal, didn't fall in love with that, it all

02:43got in the way of me. And then I discovered Assembler, and

02:48that was much more fun. And from there I went to

02:53Microcode. So you were drawn to the, you found the low-level stuff

03:00beautiful. I went through a lot of languages and

03:03then I spent significant time in Assembler and

03:08Microcode. That was sort of the first really

03:11profitable things, and I paid for my master's actually.

03:15And then I discovered Simula, which was absolutely great.

03:19Simula? Simula was the extension of Alcol 60,

03:25done primarily for simulation, but basically they invented object-oriented

03:30programming at inheritance and runtime polymorphism

03:34while they were doing it.

03:37And that was the language that taught me that you could have

03:43the sort of the problems of a program grow with size of the program rather

03:49than with the square of the size of the

03:52program. That is, you can actually modularize

03:55very nicely. And that was a surprise to me.

04:00It was also a surprise to me that a stricter type system than Pascal's

04:07was helpful, whereas Pascal's type system got in my way all the time.

04:13So you need a strong type system to organize your code well, but it has to

04:19be extensible and flexible. Let's get into the details a little bit.

04:23If you remember, what kind of type system did Pascal have?

04:27What type system, typing system, did Alcol 60 have?

04:31Basically, Pascal was sort of the simplest language that

04:36Niklaus Wirt could define that served the needs of Niklaus

04:41Wirt at the time. And it has a sort of a highly

04:46moral tone to it. That is, if you can say it

04:50in Pascal, it's good. And if you can't, it's not so good.

04:54Whereas Simula allowed you basically to build your own type system.

05:02So instead of trying to fit yourself into

05:07Niklaus Wirt's world, Christen Nygård's language and

05:12Ole Johan Dahl's language allowed you to build your own.

05:15So it's sort of close to the original idea of you build a domain-specific

05:23language. As a matter of fact, what you build is a

05:27set of types and relations among types that allows you

05:31to express something that's suitable for an

05:34application. So when you say types, stuff you're saying has echoes of

05:40object-oriented programming. Yes, they invented it. Every language

05:44that uses the word "class" for type is a descendant of

05:49Simula. Directly or indirectly.

05:55Christen Nygård and Ole Johan Dahl were mathematicians,

05:59and they didn't think in terms of types.

06:03But they understood sets and classes of elements, and so they called their types

06:09"classes." And basically in C++, as in

06:14Simula, classes are user-defined type. So can you try the impossible task and

06:20give a brief history of programming languages from your

06:25perspective? So we started with Algol 60, Simula,

06:30Pascal, but that's just the 60s and 70s. I can try.

06:36The most sort of interesting and major improvement of programming languages was

06:43Fortran, the first Fortran. Because before that,

06:47all code was written for a specific machine, and each

06:50specific machine had a language, a simply language or

06:56macro assembler or some extension of that idea.

07:00But you are writing for a specific machine

07:03in the language of that machine. And

07:09Barkas and his team at IBM built a language that

07:13would allow you to write what you really wanted.

07:17That is, you could write it in a language that was natural for people.

07:23Now these people happened to be engineers and physicists, so

07:26the language that came out was somewhat unusual for the rest of the world.

07:30But basically they said "formula translation" because they wanted to have

07:34the mathematical formulas translated into the machine. And as a

07:39side effect, they got portability. Because now they are

07:44writing in the terms that the humans used, and

07:48the way humans thought. And then they had a program that

07:52translated it into the machine's needs. And that was new, and that was

07:58great. And it's something to remember. We want to

08:02raise the language to the human level, but we don't want to lose the efficiency.

08:09And that was the first step towards the human.

08:12That was the first step. And of course, they were very particular kind of

08:16humans. Business people were different, so they

08:20got Cobol instead, and etc. And Simula came out.

08:25No, let's not go to Simula yet. Let's go to Algol.

08:30Fortran didn't have, at the time, the notions of...

08:36not a precise notion of type, not a precise notion of scope,

08:41not a set of translation phases that was what we have today.

08:49Lexical, syntax, semantics. It was sort of a bit of a model in the early days, but

08:56hey, they'd just done the biggest breakthrough in the history of

09:00programming, right? So you can't criticize them for not having gotten all the

09:04technical details right. So we got Algol. That was very pretty.

09:09And most people in commerce and science

09:14considered it useless, because it was not flexible enough, and it wasn't

09:18efficient enough, and etc. But that was a breakthrough from a

09:24technical point of view. Then Simula came along to make that idea

09:30more flexible, and you could define your own types.

09:34And that's where I got very interested.

09:39Christen Nygård, who's the main idea man behind

09:43Simula. That was late 60s. This was late 60s.

09:46Well, I was a visiting professor in Aarhus, and so I learned object-oriented

09:52programming by sitting around and, well, in theory,

09:57discussing with Christen Nygård.

10:02But Christen, once you get started and in full flow, it's very hard to get a

10:08word in edgeways. You're just listening. So it was great. I learned it from there.

10:14Not to romanticize the notion, but it seems like a big leap

10:17to think about object-oriented programming.

10:21It's really a leap of abstraction. Yes. And was that as

10:29big and beautiful of a leap as it seems from

10:33now in retrospect, or was it an obvious one

10:36at the time? It was not obvious, and many people have tried to do

10:43something like that, and most people didn't come up with

10:46something as wonderful as Simula. Lots of people got their

10:51PhDs and made their careers out of forgetting about Simula or never

10:56knowing it. For me, the key idea was basically I

11:00could get my own types. And that's the idea that goes further

11:06into C++, where I can get better types and

11:10more flexible types and more efficient types. But it's still the

11:14fundamental idea. When I want to write a program, I want to write it with my types

11:19that is appropriate to my problem and under the constraints that I'm under

11:26with hardware, software, environment, etc.

11:29And that's the key idea. People picked up on the class hierarchies

11:36and the virtual functions and the inheritance, and

11:41that was only part of it. It was an interesting and major part and still a

11:47major part in a lot of graphic stuff, but it was not the most fundamental.

11:53It was when you wanted to relate one type to another, you don't want

11:58them all to be independent. The classical example is that you

12:03don't actually want to write a city simulation with vehicles,

12:10where you say, well, if it's a bicycle, write the code for turning a bicycle to

12:15the left. If it's a normal car, turn right the

12:18normal car way. If it's a fire engine, turn right the fire engine way.

12:23You get these big case statements and bunches of if statements and such.

12:28Instead, you tell the base class that

12:34that's the vehicle and say, turn left the way you want to.

12:39And this is actually a real example. They used it to simulate

12:44and optimize the emergency services for

12:51somewhere in Norway back in the 60s. So this was one of the early examples

12:58for why you needed inheritance and you needed a runtime polymorphism.

13:06Because you wanted to handle this set of

13:09vehicles in a manageable way. You can't just rewrite your code each

13:16time a new kind of vehicle comes along.

13:19Yeah, that's a beautiful, powerful idea. And of course, it stretches through your

13:24work with C++, as we'll talk about. But I think you've structured it nicely.

13:31What other breakthroughs came along in the history of programming languages?

13:35If we were to tell the history in that way?

13:39Obviously, I'm better at telling the part of the history that

13:42that is the path I'm on, as opposed to all the paths.

13:46Yeah, you skipped the hippie John McCarthy in Lisp,

13:50one of my favorite languages. But Lisp is not one of my favorite

13:55languages. It's obviously important. It's

13:58obviously interesting. Lots of people write code in it and then

14:02they rewrite it into C or C++ when they want to go to production.

14:06It's in the world I'm at, which are constrained by performance,

14:14reliability issues, deployability,

14:19cost of hardware. I don't like things to be too dynamic.

14:26It is really hard to write a piece of code that's perfectly flexible,

14:32that you can also deploy on a small computer,

14:36and that you can also put in, say, a telephone switch

14:39in Bogota. What's the chance, if you get an error and you find yourself in the

14:44debugger, that the telephone switch in Bogota on

14:48late Sunday night has a programmer around?

14:51The chance is zero. And so a lot of things I think most about

14:58can't afford that flexibility. I'm quite aware that maybe

15:0570, 80 percent of all code are not under the kind of constraints

15:11I'm interested in. But somebody has to do the job I'm

15:16doing, because you have to get from these

15:19high-level flexible languages to the hardware.

15:23The stuff that lasts for 10, 20, 30 years is robust,

15:27operates under very constrained conditions, yes, absolutely.

15:30That's right. And it's fascinating and beautiful in its own way.

15:34C++ is one of my favorite languages, and so is Lisp. So I can

15:40embody it, too, for different reasons as a programmer.

15:47I understand why Lisp is popular, and I can see

15:51the beauty of the ideas, and similarly with

15:55Smalltalk. It's just not as relevant in my world.

16:05And by the way, I distinguish between those and the functional languages

§02Transcript · 16:09–31:37

16:09where I go to, things like ML and Haskell.

16:13Different kind of languages, they have a different kind of beauty,

16:18and they're very interesting. And I actually try to learn from

16:23all the languages I encounter to see what is there that would make

16:29working on the kind of problems I'm interested in

16:33with the kind of constraints that I'm interested in,

16:38what can actually be done better, because we can surely do better than we do today.

16:45You've said that it's good for any professional programmer to know at

16:49least five languages, speaking about a variety of languages

16:54that you've taken inspiration from. And you've listed

16:58yours as being, at least at the time, C++, obviously, Java, Python,

17:05Ruby, and JavaScript. Can you, first of all, update that list, modify it?

17:12You don't have to be constrained to just five, but can you describe what

17:17you picked up also from each of these languages, how you

17:22see them as inspirations for even you working with C++?

17:25This is a very hard question to answer. So, about languages, you should know

17:33languages. I reckon I knew about 25 or thereabouts when I did C++.

17:41It was easier in those days because the languages were smaller

17:44and you didn't have to learn a whole programming environment and such to do

17:50it. You could learn the language quite easily.

17:52And it's good to learn so many languages.

17:55I imagine, just like with natural language for communication,

18:02there's different paradigms that emerge in all of them.

18:05Yeah. That there's commonalities and so on.

18:08So, I picked five out of a hat. You picked five out of a hat.

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

18:17That's right. It's like, I don't like, I mean, if you're a monoglot, you are

18:22likely to think that your own culture is the only one

18:26superior to everybody else's. A good learning of a foreign language and a

18:30foreign culture is important. It helps you think and be a

18:34better person. With programming languages, you become a

18:37better programmer, better designer with the second language.

18:41Now, once you've got two, the wait of five is not

18:45that long. It's the second one that's most important. And then when I had to

18:51pick five, I sort of

18:56thinking what kinds of languages are there. Well, there's a

19:00really low level stuff. It's good. It's actually good to know machine code.

19:04Even still? Sorry to interrupt. Even today.

19:08The C++ optimizers write better machine code than I do.

19:13Yes. But I don't think I could appreciate them if I actually didn't

19:18understand machine code and machine architecture.

19:22At least in my position, I have to understand a bit of it.

19:26Because you mess up the cache and you're off

19:30in performance by a factor of a hundred. Right? It shouldn't be that if you are

19:35interested in either performance or the size of the computer you have to

19:40deploy. So I would go as a simpler.

19:46I used to mention C, but these days going low level

19:50is not actually what gives you the performance.

19:53It is to express your ideas so cleanly that you can think about it and the

19:58optimizer can understand what you're up to.

20:01My favorite way of optimizing these days is to throw

20:04out the clever bits and see if it still runs fast.

20:09And sometimes it runs faster. So I need the abstraction mechanisms or

20:14something like C++ to write compact high performance code.

20:20There was a beautiful keynote by Jason Turner at the CPP Con a couple of years

20:25ago where he decided he was going to

20:27program Pong on

20:32Motorola 6800 I think it was. And he says, "Well this is relevant

20:39because it looks like a microcontroller. It has specialized hardware. It has not

20:44very much memory and it's relatively slow."

20:48And so he shows in real time how he writes Pong

20:52starting with fairly straightforward low level stuff,

20:57improving his abstractions. And what he's doing

21:00he's writing C++ and it translates into

21:06into 86 Assembler which you can do with Clang and you can see it in real

21:12time. It's the Compiler Explorer which you can

21:16use on the web. And then he wrote a little program that

21:19translated 86 Assembler into

21:23Motorola Assembler. And so he types and you can see this thing

21:28in real time. Wow. You can see it in real time and even if you can't read the

21:32assembly code you can just see it. His code gets

21:35better. The code, the assembler gets smaller. He

21:40increases the abstraction level, uses C++ 11 as it were better.

21:46This code gets cleaner. It gets easier maintainable. The code shrinks

21:50and it keeps shrinking. And I could not in any reasonable amount

21:57of time write that assembler as good as the

22:01compiler generated from really quite nice modern C++.

22:06And I'll go as far as to say that the thing that looked like C

22:10was significantly uglier and smaller when it became and larger

22:18when it became machine code. So the abstractions that can be

22:24optimized are important. I would love to see that kind of

22:28visualization in larger code bases. Yeah. That might be beautiful. But you can't

22:32show a larger code base in a one hour talk and have it fit on

22:37screen. Right. So that's C and C++. So my two languages would be machine code

22:42and C++. And then I think you can learn a lot

22:47from the functional languages. So Pig has GloyML. I don't care which.

22:53I think actually you learn the same lessons

22:57of expressing especially mathematical notions really clearly

23:03and having a type system that's really strict.

23:08And then you should probably have a language for

23:11sort of quickly churning out something. You could pick JavaScript. You could

23:17pick Python. You could pick Ruby. What do you make of JavaScript in general?

23:22So you're talking in the platonic sense about languages, about

23:27what they're good at, what their philosophy of design is. But

23:31there's also a large user base behind each of these languages and they use it

23:36in the way sometimes maybe it wasn't really designed

23:39for. That's right. JavaScript is used way beyond

23:42probably what it was designed for. Let me say it this way. When you build a

23:46tool, you do not know how it's going to be used.

23:49You try to improve the tool by looking at how it's being used and when people

23:54cut their fingers or from trying to stop that from happening.

23:58But really you have no control over how something is used.

24:02So I'm very happy and proud of some of the things C++ is being used at

24:07and some of the things I wish people wouldn't do.

24:10Bitcoin mining being my favorite example. It uses as much energy as Switzerland

24:16and mostly serves criminals. Yeah. But back to the languages.

24:23I actually think that having JavaScript run in the browser

24:28was an enabling thing for a lot of things. Yes, you could have

24:33done it better, but people were trying to do it better

24:36and they were using

24:39sort of more principled language designs, but they just couldn't do it right.

24:44And the non-professional programmers that write

24:49lots of that code just couldn't understand them. So

24:53it did an amazing job for what it was. It's not the prettiest

25:00language and I don't think it ever will be

25:02the prettiest language, but let's not be bigots here.

25:07So what was the origin story of C++? You basically gave a few

25:14perspectives of your inspiration of object-oriented

25:19programming. You had a connection with C and

25:23performance efficiency was an important thing you

25:26were drawn to. Efficiency and reliability. Reliability.

25:30You have to get both. What's reliability? I

25:35really want my telephone calls to get through

25:39and I want the quality of what I am talking coming out at the other end.

25:44The other end might be in London or wherever.

25:49And you don't want the system to be crashing.

25:53If you're doing a bank, you mustn't crash.

25:57It might be your bank account that is in trouble. There's different

26:02constraints like in games, it doesn't matter too much if there's a crash.

26:06Nobody dies and nobody gets ruined, but I'm interested in the combination of

26:11performance, partly because of sort of

26:16speed of things being done, part of being able to do things that is

26:21necessary to have reliability

26:26of larger systems. If you spend all your time

26:31interpreting a simple function call, you are not going to have enough time to

26:37do proper signal processing to get the telephone calls to sound right.

26:42Either that or you have to have 10 times as many computers and you can't afford

26:46your phone anymore. It's a ridiculous idea in the modern

26:51world because we have solved all of those problems.

26:55I mean they keep popping up in different ways because we

26:58tackle bigger and bigger problems, so efficiency remains always an important

27:02aspect. But you have to think about efficiency not just

27:06as speed but as an enabler to important things and one of the things

27:12it enables is reliability, is dependability.

27:18When I press the pedal, the brake pedal of a car,

27:22it is not actually connected directly to

27:26anything but a computer. That computer better work.

27:31Let's talk about reliability just a little bit. So

27:34modern cars have ECUs, have millions of lines of code today.

27:42So this is certainly especially true of autonomous vehicles where some of the

27:46aspects of the control or driver assistance systems that steer

27:49the car, keeping the lanes on. So how do you think, you know, I talk to

27:54regulators, people in government who are very nervous about testing the

27:59safety of these systems of software. Ultimately software that makes

28:04decisions that could lead to fatalities. So

28:09how do we test software systems like these?

28:13First of all, safety like performance and like security

28:21is the system's property. People tend to look at one part of a system at a time

28:26and saying something like, "This is secure." That's

28:30all right. I don't need to do that. Yeah, that piece of code is secure. I'll

28:35buy your operator. If you want to have

28:39reliability, if you want to have performance, if you want to have

28:43security, you have to look at the whole system.

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

28:50I'm dealing with one part of the system and I want my part to be

28:53really good, but I know it's not the whole system.

28:57Furthermore, making an individual part perfect

29:04may actually not be the best way of getting the highest degree of

29:07reliability and performance and such. There's people who say C++ is type safe,

29:13not type safe. You can break it. Sure. I can break anything that runs on a

29:18computer. I may not go through your type system.

29:23If I wanted to break into your computer, I'll probably try SQL injection.

29:28It's very true. If you think about safety or even reliability at a system level,

29:34especially when a human being is involved,

29:38it starts becoming hopeless pretty quickly in terms of

29:45proving that something is safe to a certain level.

29:49Because there's so many variables, it's so complex. Well, let's get back to

29:53something we can talk about and actually make some progress on.

29:57We can look at C++ programs and we can

30:01try and make sure they crash less often. The way you do that

30:09is largely by simplification. It is not... the first step

30:15is to simplify the code, have less code, have code that are less likely to go

30:21wrong. It's not by runtime testing everything.

30:24It is not by big test frameworks that you're using.

30:29Yes, we do that also. But the first step is actually to make sure that when you

30:35want to express something, you can express it directly in code

30:40rather than going through endless loops and convolutions in your head

30:45before it gets down the code. That if the way you are thinking about a

30:51problem is not in the code. There is a missing

30:56piece that's just in your head. And the code,

30:59you can see what it does, but it cannot see what you

31:03thought about it unless you have expressed things

31:06directly. When you express things directly, you can

31:10maintain it. It's easier to find errors. It's easier

31:13to make modifications. It's actually easier to test it. And

31:18lo and behold, it runs faster. And therefore you can use a

31:23smaller number of computers, which means there's less

31:26hardware that could possibly break. So I think the key here is

31:32simplification. But it has to be, to use the Einstein

31:37quote, as simple as possible and no simpler.

§03Transcript · 31:40–48:13

31:40There are other areas with under constraint where you can be

31:44simpler than you can be in C++, but in the domain I'm dealing with,

31:50that's the simplification I'm after. So how do you inspire or

31:57ensure that the Einstein level of simplification is reached?

32:03So can you do code review? Can you look at code?

32:08Is there, if I gave you the code for the Ford F-150 and said,

32:13here, is this a mess or is this okay? Is it possible to tell?

32:20Is it possible to regulate? An experienced developer can look

32:26at code and see if it smells. I mix metaphors deliberately.

32:31The point is that it is hard to generate something that is

32:43really obviously clean and can be appreciated, but you can

32:50usually recognize when you haven't reached that point.

32:53And so if I, I've never looked at the F-150 code, so I wouldn't know.

33:03But I know what I would be looking for. I'll be looking for some

33:07tricks that correlate with bugs and elsewhere.

33:10And I have tried to formulate rules for what good code looks like.

33:19And the current version of that is called the C++ core guidelines.

33:26One thing people should remember is there's what you can do

33:31in a language and what you should do. In a language, you have lots of things that

33:38is necessary in some contexts, but not in others.

33:42There's things that exist just because there's

33:4530-year-old code out there and you can't get rid of it.

33:48But you can't have rules that says when you create it, try and follow these rules.

33:54This does not create good programs by themselves,

34:00but it limits the damage and for mistakes,

34:03it limits the possibilities of mistakes. And basically, we are trying to

34:08say what is it that a good programmer does

34:12at the fairly simple level of where you use the language and how you use it.

34:17Now, I can put all the rules for chiseling in marble. It doesn't mean

34:24that somebody who follows all of those rules

34:27can do a masterpiece by Michelangelo. That is, there's something else to write

34:35a good program. Just is there something else to create

34:38an important work of art. That is, there's some kind of

34:44inspiration, understanding, gift. But we can

34:51approach the sort of technical, the craftsmanship level of it.

34:59The famous painters, the famous sculptures,

35:02was among other things, superb craftsman. They could express their ideas

35:10using their tools very well. And so these days, I think what I'm doing,

35:18what a lot of people are doing, we are still trying to figure out how it

35:21is to use our tools very well. For a really good piece of code,

35:28you need a spark of inspiration and you can't, I think, regulate that.

35:33You cannot say that I'll take a picture only,

35:38I'll buy your picture only if you're at least Van Gogh.

35:44There are things you can regulate, but not the inspiration.

35:50I think that's quite beautifully put. It is true

35:54that there is, as an experienced programmer, when you see code that's

36:00inspired, that's like Michelangelo,

36:05you know it when you see it. And the opposite of that is code that

36:11is messy, code that smells, you know when you see it.

36:14And I'm not sure you can describe it in words except

36:18vaguely through guidelines and so on. Yes, it's

36:21easier to recognize ugly than to recognize beauty

36:26in code. And for the reason is that sometimes

36:30beauty comes from something that's innovative and unusual.

36:34And you have to sometimes think reasonably hard to appreciate that.

36:38On the other hand, the messes have things

36:42in common. And you can have static checkers and dynamic

36:48checkers that find a large number of the

36:55most common mistakes. You can catch a lot of sloppiness mechanically. I'm a

37:02great fan of static analysis in particular

37:07because you can check for not just the language rules but for the usage of

37:11language rules. And I think we will see much more

37:15static analysis in the coming decade. Can you describe

37:19what static analysis is? You represent a piece of code

37:25so that you can write a program that goes over

37:30that representation and look for things that are

37:35right and not right. So for instance you can

37:39analyze a program to see if

37:45resources are leaked. That's one of my favorite

37:50problems. It's not actually all that hard in modern C++ but you can do it.

37:56If you are writing in the C level you have to have a malloc and a free

38:01and they have to match. If you have them in a single function you can

38:07usually do it very easily. If there's a malloc here there should be a free there.

38:14On the other hand, in between can be true and complete code and then it becomes

38:18impossible. If you pass that pointer to the

38:22memory out of a function and then want to make sure that the free

38:29is done somewhere else. Now it gets really difficult.

38:33And so for static analysis you can run through a program

38:37and you can try and figure out if there's any leaks.

38:42And what you will probably find is that you will find some leaks

38:48and you'll find quite a few places where your analysis can't be complete.

38:53It might depend on runtime. It might depend

38:56on the cleverness of your analyzer. And it might take a long time. Some of

39:03these programs run for a long time. But if you combine

39:10such analysis with a set of rules that says how

39:14people could use it, you can actually see why the rules are violated.

39:19And that stops you from getting into the impossible complexities. You don't want

39:25to solve the holding problem.

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

39:32Yes. And thereby it's almost, not in production code, but it's almost

39:38like an educational tool of how the language should be used.

39:43It guides you. At its best, it would guide you in how you write future

39:50code as well and you learn together.

39:52Yes. So basically you need a set of rules for how you use the language.

39:56Then you need a static analysis that catches your mistakes when you violate

40:04the rules or when your code ends up doing things that it shouldn't, despite

40:09the rules, because there is the language rules. We can go further.

40:13And again, it's back to my idea that I would much rather find errors before I

40:18start running the code. If nothing else, once the code runs, if

40:24it catches an error at run times, I have to have an error handler.

40:28And one of the hardest things to write in code is error handling code, because

40:33you know something went wrong. Do you know really exactly what went

40:38wrong? Usually not. How can you recover when you

40:41don't know what the problem was? You can't be 100% sure what the problem

40:46was in many, many cases. And this is part of it. So yes, we need

40:54good languages with good type systems. We need rules for how to use them.

40:58We need static analysis. And the ultimate for static analysis is,

41:03of course, program proof, but that still doesn't scale to the kind

41:07of systems we deploy. Then we start needing testing and

41:13the rest of the stuff.

41:15So C++ is an object-oriented programming language that creates,

41:21especially with its newer versions, as we'll talk about, higher and higher

41:24levels of abstraction. So how do you design...

41:30Let's even go back to the origin of C++. How do you design something with so much

41:34abstraction that's still efficient and

41:39is still something that you can manage, do static analysis on, you can

41:47have constraints on, that can be reliable, all those things we've talked about.

41:51So to me, there's a slight tension between

41:58high-level abstraction and efficiency. That's a good question. I could probably

42:03have a year's course just trying to answer it.

42:08Yes, there's a tension between efficiency and abstraction,

42:12but you also get the interesting situation that you get the best

42:17efficiency out of the best abstraction. And my main tool

42:23for efficiency, for performance, actually is abstraction.

42:28So let's go back to how C++ got there.

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

42:35said that. It's always quoted, but I never did. I

42:39said C++ supports object-oriented programming and

42:44other techniques. And that's important, because I think

42:48that the best solution to most

42:52complex, interesting problems require ideas and techniques from

42:59things that have been called object-oriented,

43:04data abstraction, functional, traditional C-style code,

43:11all of the above. And so when I was designing C++,

43:18I soon realized I couldn't just add features.

43:23If you just add what looks pretty, or what people ask for,

43:27or what you think is good, one by one, you're not going to get a

43:31coherent whole. What you need is a set of guidelines

43:36that guides your decisions. Should this feature be in, or

43:41should this feature be out? How should a feature be modified before

43:46it can go in, and such? And there's a, in the book I wrote

43:50about that, that's "Sign Evolution of C++," there's a

43:54whole bunch of rules like that. Most of them are not

43:57language technical. They're things like,

44:02"Don't violate static type system," because I like static type system

44:07for the obvious reason that I like things to be reliable on

44:13reasonable amounts of hardware. But one of these rules is,

44:19it's a zero overhead principle. The what kind of principle? The zero overhead

44:23principle. It basically says that if you

44:28have an abstraction, it should not cost anything compared to write

44:34the equivalent code at a lower level.

44:39So if I have, say, a matrix multiply, it should be written in such a way

44:48that you could not drop to the C level of abstraction and use arrays and

44:53pointers and such and run faster. And so people have written such

45:00matrix multiplications, and they've actually gotten

45:04code that ran faster than Fortran, because once you had the right

45:08abstraction, you can eliminate, you can eliminate temporaries, and you

45:13can do loop fusion and other good stuff like

45:17that. That's quite hard to do by hand and in a

45:20lower level language. And there's some really nice examples of

45:24that. And the key here is that that matrix

45:29multiplication, the matrix abstraction, allows you to write code that's simple

45:35and easy. You can do that in any language.

45:37But with C++, it has the features so that you can also

45:41have this thing run faster than if you hand-coded it.

45:45Now, people have given that lecture many times, I and others,

45:50and a very common question after the talk, where you have demonstrated that

45:54you can outperform Fortran for dense matrix multiplication, people

45:59come up and say, "Yeah, but that was C++.

46:01If I rewrote your code in C, how much faster would it run?"

46:06The answer is, much slower. This happened the first time, actually,

46:12back in the '80s, with a friend of mine called Doug McElroy,

46:15who demonstrated exactly this effect.

46:20And so, the principle is, you should give programmers the tools so that their

46:26abstractions can follow the zero-overhead principle.

46:30Furthermore, when you put in a language feature in C++,

46:34or a standard library feature, you try to meet this.

46:38It doesn't mean it's absolutely optimal, but it means if you hand-code it

46:43with the usual facilities in the language,

46:46in C++, in C, you should not be able to better it.

46:51Usually, you can do better if you use embedded assembler for machine code,

46:57for some of the details to utilize part of a computer that the compiler doesn't

47:03know about. But you should get to that point before

47:06you beat to the abstraction.

47:09So, that's a beautiful ideal to reach for.

47:12And we meet it quite often.

47:16So, where's the magic of that coming from?

47:19There's some of it is the compilation process, so the implementation of C++.

47:23Some of it is the design of the feature itself,

47:27the guidelines. So, I've recently, and often, talked to Chris Latner,

47:33so, Clang. What, just out of curiosity, is your

47:40relationship in general with the different implementations of C++

47:44as you think about you and committee and other people in C++, think about the

47:49design of new features or design of previous features.

47:54In trying to reach the ideal of zero overhead,

47:59does the magic come from the design, the guidelines, or from the

48:05implementations?

48:06And. Not all.

48:09You go for programming technique,

48:13program language features, and implementation techniques. You need all

§04Transcript · 48:17–1:04:14

48:17three.

48:18And how can you think about all three at the same time?

48:23It takes some experience, takes some practice, and sometimes you get it wrong.

48:27But after a while, you sort of get it right.

48:31I don't write compilers anymore, but

48:36Brian Kernighan pointed out that one of the reasons C++

48:42succeeded was some of the craftsmanship I put into the

48:48early compilers. And, of course, I did the language

48:52design, and of course, I wrote a fair amount of code using this kind of

48:55stuff. And I think most of the successes

49:00involve progress in all three areas together.

49:05A small group of people can do that. Two, three people

49:09can work together to do something like that. It's ideal if it's one person

49:13that has all the skills necessary, but nobody has all the skills necessary

49:18in all the fields where C++ is used. So if you want to

49:22approach my ideal in, say, concurrent programming, you need to

49:27know about algorithms from concurrent programming.

49:30You need to know the trigger of lock-free programming.

49:34You need to know something about compiler techniques.

49:37And then you have to know some of the program areas,

49:41sorry, the application areas where this is,

49:45like some forms of graphics or some forms of

49:51what we call a web-serving kind of stuff. And that's very hard to get

49:57into a single head, but small groups can do it too.

50:01So is there differences in your view, not saying which is better or so on,

50:08but differences in the different implementations

50:10of C++? Why are there several sort of maybe naive questions for me?

50:18GCC, Clang, so on. This is a very reasonable question.

50:23When I designed C++,

50:27most languages had multiple implementations.

50:32Because if you run on an IBM, if you run on a Sun, if you run on a Motorola,

50:39there was just many, many companies and they each have their own compilation

50:42structure and their old compilers. It was just fairly common that there

50:47was many of them. And I wrote C front assuming that other

50:52people would write compilers for C++ if I was successful.

50:57And furthermore, I wanted to utilize all the back-end infrastructures that

51:04were available. I soon realized that my users were using

51:0725 different linkers. I couldn't write my own linker.

51:12Yes, I could, but I couldn't write 25 linkers and also get any work done on

51:18the language. And so it came from a world where there

51:22was many linkers, many optimizers, many

51:27compiler front-ends, not to start, but many operating systems.

51:34The whole world was not an 86 and a Linux box or something,

51:39whatever is the standard today. In the old days, they said a set of

51:43backs. So basically, I assumed there would be

51:47lots of compilers. It was not a decision that there should

51:51be many compilers. It was just a fact. That's the way the

51:55world is. And yes,

51:59many compilers emerged. And today, there's at least four front-ends,

52:08Clang, GCC, Microsoft, and EDG. It is the same group.

52:15They supply a lot of the independent organizations and the embedded systems

52:22industry. And there's lots and lots of back-ends.

52:26We have to think about how many dozen back-ends there are.

52:31Because different machines have different things. Especially in the

52:35embedded world, the machines are very different. The architectures are very

52:39different. And so,

52:43having a single implementation was never an option. Now, I also happen to

52:50dislike monocultures. Monocultures?

52:54They are dangerous. Because whoever owns the

52:58monoculture can go stale, and there's no competition,

53:03and there's no incentive to innovate. There's a lot of incentive to put

53:08barriers in the way of change. Because, hey, we own the world, and it's

53:13a very comfortable world for us. And who are you to

53:16to mess with that? So, I really am very happy that there's four

53:23front-ends for C++. Clang's great, but GCC was great.

53:30But then it got somewhat stale. Clang came along,

53:34and GCC is much better now. Competition is good.

53:38Microsoft is much better now. So, at least a low number of front-ends

53:46puts a lot of pressure on

53:49standards compliance, and also on performance, and error messages,

53:55and compile time, speed, all this good stuff that we want.

54:01Do you think, crazy question, there might come along,

54:05do you hope there might come along, implementation of C++

54:10written, given all its history, written from scratch? So, written today

54:18from scratch? Well, Clang and LLVM is more or less written by from scratch.

54:24But there's been C++ 11, 14, 17, 20, you know, there's been a lot of...

54:30Sooner or later, somebody's going to try again.

54:33There has been attempts to write new C++ compilers, and

54:39some of them has been used, and some of them has been absorbed into others, and

54:42such. Yeah, it'll happen. So, what are the key features of C++?

54:49And let's use that as a way to sort of talk about

54:54the evolution of C++, the new feature. So,

54:57at the highest level, what are the features that were there in the

55:01beginning? What features got added? Let's first get a principle,

55:07an aim in place. C++ is for people who want to use

55:14hardware really well, and then manage the complexity of doing that through

55:19abstraction. And so, the first facility

55:24you have is a way of manipulating the machines at a fairly low level. That

55:31looks very much like C. It has loops, it has variables, it

55:38has pointers, like machine addresses, it can access memory directly, it can

55:44allocate stuff in the absolute minimum of space

55:49needed on the machine. There's a machine-facing part of C++,

55:54which is roughly equivalent to C. I said C++ could beat C, and it can.

56:00It doesn't mean I dislike C. If I disliked C,

56:03I wouldn't have built on it. Furthermore, after Dennis Ritchie, I'm

56:09probably the major contributor to modern C.

56:13And, well, I had lunch with Dennis most days for 16 years, and we never

56:21had a harsh word between us. So,

56:25these C versus C++ fights are for people who don't quite understand

56:30what's going on. Then the other part is the abstraction.

56:35And there, the key is the class, which is a user-defined type.

56:40And my idea for the class is that you should be able to build a type

56:44that's just like the built-in types, in the way you use them, in the way you

56:50declare them, in the way you get the memory,

56:53and you can do just as well. So, in C++, there's an int,

56:59as in C. You should be able to build an abstraction, a class, which we can call

57:05capital int, that you can use exactly like an integer

57:10and run just as fast as an integer. There's the idea right there. And, of

57:16course, you probably don't want to use the int

57:19itself, but it has happened. People have wanted integers that were

57:25range-checked so that you couldn't overflow and such, especially for very

57:28safety-critical applications, like the fuel injection for a

57:33marine diesel engine for the largest ships.

57:37This is a real example, by the way. This has been done.

57:40They built themselves an integer that was just like integer,

57:45except that it couldn't overflow. If there was an overflow, you went into the error

57:49handling. And then you built more interesting

57:54types. You can build a matrix, which you need to do graphics,

58:00or you could build a gnome for a video game.

58:04And all these are classes and they appear just like the built-in types?

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

58:11And flexibility. So, I don't know. For people who are not

58:18familiar with object-oriented programming, there's inheritance.

58:21There's a hierarchy of classes. You can,

58:24just like you said, create a generic vehicle that can turn left.

58:29So, what people found was that you don't actually...

58:36No, how do I say this? A lot of types are related.

58:43That is, the vehicles, all vehicles are related.

58:48Bicycles, cars, fire engines, tanks. They have some things in common and

58:55some things that differ. And you would like to have the common

58:58things common and having the differences specific.

59:03And when you didn't want to know about the differences, like,

59:06just turn left. You don't have to worry about it. That's how you get

59:12the traditional object-oriented programming coming out of Simula,

59:16adopted by Smalltalk and C++ and all the other languages.

59:21The other kind of obvious similarity between types

59:25comes when you have something like a vector.

59:29Fortran gave us the vector, called array,

59:32of doubles. But the minute you have a vector of doubles, you want a vector

59:39of double precision doubles, and for short doubles, for graphics.

59:45Why should you not have a vector of integers while you're at it?

59:49Or a vector of vectors, a vector of vectors of chess pieces.

59:55Now you have a board, right? So, this is, you express the commonality

60:03as the idea of a vector, and the variations come through parameterization.

60:10And so, here we get the two fundamental ways of abstracting,

60:14of having similarities of types in C++.

60:21There's the inheritance, and there's a parameterization.

60:24There's the object-oriented programming, and there's the generic programming.

60:28With the templates for the generic programming?

60:30Yep.

60:31So, you've presented it very nicely, but now you have to make all that happen

60:38and make it efficient. So, generic programming,

60:42with templates, there's all kinds of magic going on, especially recently,

60:47that you can help catch up on. But it feels to me like you can do way more

60:52than what you just said, with templates. You can start doing

60:56this kind of metaprogramming, this kind of...

60:58You can do metaprogramming also. I didn't go there in that explanation.

61:04We're trying to be very basic, but go back on to the implementation.

61:08Implementation.

61:08If you couldn't implement this efficiently, if you couldn't use it

61:13so that it became efficient, it has no place in C++,

61:17because it will violate the zero overhead principle.

61:20So, when I had to get object-oriented programming inheritance,

61:27I took the idea of virtual functions from simula.

61:32Virtual functions is a simula term. Class is a simula term.

61:37If you ever use those words, say thanks to Christian Nygaard

61:40and Ole Johan Dahl. And I did the simplest implementation

61:46I knew of, which was basically a jump table.

61:50So, you get the virtual function table, the function goes in,

61:55does an indirection through a table, and get the right function.

61:59That's how you pick the right thing there. And I thought that was trivial.

62:04It's close to optimal. And it was obvious.

62:08It turned out the simula had a more complicated way of doing it,

62:12and therefore slower. And it turns out that most languages

62:16have something that's a little bit more complicated,

62:18sometimes more flexible, but you pay for it.

62:21And one of the strengths of C++ was that you could actually do

62:25this object-oriented stuff, and your overhead compared to

62:31ordinary functions, there's no indirection, it's sort of in 5, 10, 25 percent.

62:37Just the call. It's down there. It's not two.

62:41And that means you can afford to use it. Furthermore, in C++,

62:47you have the distinction between a virtual function and a non-virtual function.

62:51If you don't want any overhead, if you don't need the indirection

62:56that gives you the flexibility in object-oriented programming,

62:59just don't ask for it. So the idea is that you only use

63:04virtual functions if you actually need the flexibility.

63:07So it's not zero overhead, but it's zero overhead compared

63:12to any other way of achieving the flexibility.

63:14Now, auto-parameterization.

63:18Basically, the compiler looks at the template,

63:28say the vector, and it looks at the parameter,

63:34and then combines the two and generates a piece of code

63:38that is exactly as if you've written a vector of that specific type.

63:43So that's the minimal overhead. If you have many template parameters,

63:50you can actually combine code that the compiler couldn't usually see

63:54at the same time, and therefore get code that is faster

63:59than if you had handwritten the stuff, unless you are very, very clever.

64:05So the thing is, parameterized code, the compiler fills stuff in

64:11during the compilation process, not during runtime.

64:14That's right. And furthermore, it gives all the information it's gotten,

§05Transcript · 1:04:20–1:17:19

64:20which is the template, the parameter, and the context of use.

64:26It combines the three and generates good code.

64:29But it can generate...

64:32Now, it's a little outside of what I'm even comfortable thinking about,

64:37but it can generate a lot of code.

64:39Yes.

64:40And how do you... I remember being both amazed at the power of that idea,

64:48and how ugly the debugging looked.

64:52Yes, debugging can be truly horrid.

64:56Come back to this, because I have a solution.

64:58Anyway, the debugging was ugly.

65:02The code generated by C++ has always been ugly,

65:09because there's these inherent optimizations.

65:12A modern C++ compiler has front-end, middle-end, and back-end optimizations.

65:17Even C-Front, back in '83, had front-end and back-end optimizations.

65:23I actually took the code, generated an internal representation,

65:29munched that representation to generate good code.

65:33So people say, "This is not a compiler that generates C."

65:36The reason it generated C was I wanted to use C's code generators

65:41that was really good at back-end optimizations.

65:43But I needed front-end optimizations,

65:46and therefore the C I generated was optimized C.

65:51The way a really good handcrafted optimizer, human, could generate it,

65:59and it was not meant for humans.

66:01It was the output of a program, and it's much worse today.

66:05And with templates, it gets much worse still.

66:07So it's hard to combine simple debugging with optimal code,

66:16because the idea is to drag in information from different parts of the code

66:22to generate good code, machine code.

66:26And that's not readable.

66:29So what people often do for debugging is they turn the optimizer off.

66:35And so you get code that, when something in your source code

66:42looks like a function call, it is a function call.

66:45When the optimizer is turned on, it may disappear, the function call.

66:50It may inline.

66:51And so one of the things you can do is you can actually get code

66:57that is smaller than the function call,

67:01because you eliminate the function preamble and return,

67:05and there's just the operation there.

67:08One of the key things when I did templates was

67:14I wanted to make sure that if you have, say, a sort algorithm,

67:19and you give it a sorting criteria,

67:23if that sorting criteria is simply comparing things with less than,

67:30the code generated should be the less than,

67:34not an indirect function call to a comparison object,

67:40which is what it is in the source code.

67:44But we really want down to the single instruction.

67:47And but anyway, turn off the optimizer, and you can debug.

67:53The first level of debugging can be done,

67:56and I always do without the optimization on,

67:59because then I can see what's going on.

68:01- And then there's this idea of concepts that puts some...

68:06Now, I've never even...

68:11I don't know if it was ever available in any form,

68:14but it puts some constraints on the stuff you can parameterize, essentially.

68:19- Let me try and explain.

68:21So yes, it wasn't there 10 years ago.

68:27We have had versions of it that actually work for the last four or five years.

68:33It was a design by Gabby Dos Reis, Drew Sautin, and me.

68:40We were professors and postdocs in Texas at the time.

68:44And the implementation by Andrew Sautin has been available for that time.

68:53And it is part of C++20.

68:58And there's a standard library that uses it.

69:02So this is becoming really very real.

69:06It's available in Clang and GCC, GCC for a couple of years.

69:13And I believe Microsoft is soon going to do it.

69:16We expect all of C++20 to be available in all the major compilers in 20.

69:23But this kind of stuff is available now.

69:28I'm just saying that because otherwise people might think

69:32I was talking about science fiction.

69:34And so what I'm going to say is concrete, you can run it today.

69:38And there's production uses of it.

69:41So the basic idea is that when you have a generic component, like a sort function,

69:51the sort function will require at least two parameters.

69:56One, a data structure with a given type and a comparison criteria.

70:04And these things are related, but obviously you can't compare things

70:08if you don't know what the type of things you compare.

70:11And so you want to be able to say, I'm going to sort something.

70:18And it is to be sortable.

70:20What does it mean to be sortable?

70:21You look it up in the standard.

70:23It has to be a sequence with a beginning and an end.

70:27There has to be random access to that sequence.

70:31And there has to be, the element types has to be comparable.

70:37Which means less than operator can operate on it.

70:40Yes.

70:41Less than logical operator can operate on it.

70:42Basically what concepts are, they're compile time predicates.

70:47They're predicates you can ask, are you a sequence?

70:49Yes.

70:51I have a begin and end.

70:53Are you a random exit sequence?

70:55Yes.

70:56I have a subscripting and plus.

70:59Is your element type something that has a less than?

71:02Yes.

71:02I have a less than.

71:03It's and so basically that's the system.

71:07And so instead of saying, I will take a parameter of any type,

71:11it'll say, I'll take something that's sortable.

71:13And it's well defined.

71:17And so we say, okay, you can sort with less than.

71:20I don't want less than.

71:21I want greater than or something I invent.

71:24So you have two parameters, the sortable thing and the

71:27comparison criteria and the comparison criteria will say,

71:31well, I can, you can write it saying it should operate on the

71:37element type and it has the comparison operations.

71:42So that's simply the fundamental thing.

71:45It's compile time predicates.

71:47Do you have the properties I need?

71:48So it specifies the requirements of the code on the parameters

71:54that it gets, it's very similar to types actually.

71:58But operating in the space of concepts.

72:03Concepts.

72:04The word concept was used by Alex Stefanov, who is sort of

72:10the father of generic programming in the context of C++.

72:14There's other places that use that word, but the way we call

72:20generic programming is the way we call it.

72:22It's that word, but the way we call generic programming is

72:25Alex's and he called them concepts because he said they're

72:28the sort of the fundamental concepts of an area.

72:32So they should be called concepts.

72:33And we've had concepts all the time.

72:36If you look at the K&R book about C, C has arithmetic types

72:41and it has integral types.

72:47It says so in the book.

72:49And then it lists what they are and they have certain properties.

72:53The difference today is that we can actually write a concept

72:57that will ask a type, are you an integral type?

73:00Do you have the properties necessary to be an integral type?

73:05Do you have plus minus divide and such?

73:07So maybe the story of concepts, because I thought it might be

73:16part of C++11.

73:17C-O-X or whatever it was at the time.

73:22What was the, why didn't it, like what, we'll talk a little

73:28bit about this fascinating process of standards because I

73:30think it's really interesting for people.

73:32It's interesting for me, but why did it take so long?

73:37What shapes did the idea of concepts take?

73:40What were the challenges?

73:42- Back in '87 or thereabouts.

73:46- 1987?

73:48- In 1987 or thereabouts, when I was designing templates,

73:52obviously I wanted to express the notion of what is required

73:57by a template of its arguments.

74:00And so I looked at this and basically for templates,

74:04I wanted three properties.

74:06I wanted to be very flexible.

74:10It had to be able to express things I couldn't imagine

74:16because I know I can't imagine everything and I've been

74:19suffering from languages that try to constrain you to only do

74:24what the designer thought good.

74:26I didn't want to do that.

74:28Secondly, it had to run faster, as fast or faster than

74:33handwritten code.

74:35So basically if I have a vector of T and I take a vector of

74:39char, it should run as fast as you build a vector of char

74:44yourself without parameterization.

74:46And thirdly, I wanted to be able to express the constraints

74:53of the arguments, have proper type checking of the

74:59interfaces.

75:00And neither I nor anybody else at the time knew how to get

75:05all three.

75:06And I thought for C++, I must have the two first.

75:10Otherwise it's not C++.

75:13And it bothered me for another couple of decades that I

75:17couldn't solve the third one.

75:18I mean, I was the one that put function argument type checking

75:23into C.

75:24I know the value of good interfaces.

75:26I didn't invent that idea.

75:28It's very common, but I did it.

75:30And I wanted to do the same for templates, of course, and I

75:35couldn't.

75:36So it bothered me.

75:38Then we tried again, 2002, 2003.

75:43Gabby Desrais and I started analyzing the problem, explained

75:48possible solutions.

75:50It was not a complete design.

75:52A group in University of Indiana, an old friend of mine,

75:58they started a project at Indiana and

76:04we thought we could get a good system of concepts in another

76:13two or three years.

76:14That would have made C++ 11 to C++ 06 or 07.

76:23Well, it turns out that I think we got a lot of the fundamental

76:29ideas wrong.

76:31They were too conventional.

76:32They didn't quite fit C++ in my opinion.

76:37Didn't serve implicit conversions very well.

76:40It didn't serve mixed type arithmetic, mixed type

76:45computations very well.

76:48A lot of stuff came out of the functional community and that

76:55community didn't deal with multiple types in the same way

77:04as C++ does.

77:05Had more constraints on what you could express and didn't have

77:12the draconian performance requirements.

77:15And basically, we tried.

77:18We tried very hard.

77:19We had some successes, but it just in the end wasn't.

§06Transcript · 1:17:23–1:28:17

77:23Didn't compile fast enough, was too hard to use, and didn't run

77:29fast enough unless you had optimizers that was beyond the

77:35state of the art.

77:36They still are.

77:37So we had to do something else.

77:39Basically, it was the idea that a set of parameters has defined

77:46a set of operations and you go through an indirection table

77:50just like for virtual functions, and then you try to optimize

77:54the indirection away to get performance.

77:58And we just couldn't do all of that.

78:01But get back to the standardization.

78:05We are standardizing C++ under ISO rules, which are very open

78:11process.

78:12People come in, there's no requirements for education or

78:16experience.

78:17So you've started to develop C++, and there's a whole...

78:23What was the first standard established?

78:26What is that like?

78:26The ISO standard, is there a committee that you're referring

78:31to?

78:31Sure.

78:31There's a group of people.

78:32What's that like?

78:33How often do you meet?

78:35What's the discussion?

78:36I'll try and explain that.

78:38So sometime in early 1989, I think, I think it was.

78:47In 1989, two people, one from IBM, one from HP, turned up in

78:56my office and told me I would like to standardize C++.

79:01This was a new idea to me, and I pointed out that it wasn't

79:08finished yet.

79:10It wasn't ready for formal standardization and such.

79:13And they say, "No, Bjarne, you haven't gotten it.

79:15Our organizations depend on C++.

79:19We cannot depend on something that's owned by another

79:23corporation that might be a competitor.

79:26Of course, we could rely on you, but you might get run over by

79:31a bus."

79:31Right.

79:32We really need to get this out in the open.

79:36It has to be standardized under formal rules, and we are going

79:43to standardize it under ISO rules.

79:48And you really want to be part of it because basically,

79:51otherwise, we'll do it ourselves.

79:53And we know you can do it better.

79:56So through a combination of arm twisting and flattery, it got

80:05started.

80:05So in late '89, there was a meeting in D.C.

80:13Actually, no, it was not ISO then.

80:17It was ANSI, the American National Standard, we're doing.

80:20We met there.

80:24We were lectured on the rules of how to do an ANSI standard.

80:28There was about 25 of us there, which apparently was a new

80:32record for that kind of meeting.

80:35And some of the old C guys that has been standardizing C was

80:40there, so we got some expertise in.

80:42So the way this works is that it's an open process.

80:46Anybody can sign up if they pay the minimal fee, which is

80:51about $1,000.

80:52There was less then, just a little bit more now.

80:55And I think it's $1,280.

80:58It's not going to kill you.

81:01And we have three meetings a year.

81:05This is fairly standard.

81:07We try two meetings a year for a couple of years that didn't

81:12work too well.

81:12So three one-week meetings a year.

81:17And you meet and you have technical discussions, and then

81:25you bring proposals forward for votes.

81:28The votes are done one vote per organization.

81:35So you can't have, say, IBM come in with 10 people and

81:40dominate things.

81:41That's not allowed.

81:42And these are organizations that extend to the UC++?

81:45Yes.

81:46Or individuals.

81:48Or individuals.

81:49I mean, it's a bunch of people in a room deciding the design

81:54of a language based on which a lot of the world's systems

81:59run.

82:00That's right.

82:00Well, I think most people would agree it's better than if

82:04I decided it.

82:05Or better than if a single organization like AT&T decided

82:11it.

82:11I don't know if everyone agrees to that, by the way.

82:14Bureaucracies have their critics too.

82:17Yes.

82:18Look, standardization is not pleasant.

82:23It's horrifying.

82:25It's like democracy.

82:26But we, exactly.

82:28As Churchill says, democracy is the worst way except for all

82:31the others.

82:32Right?

82:33And it's, I would say, the same with formal standardization.

82:36But anyway, so we meet and we have these votes, and that

82:43determines what the standard is.

82:44A couple of years later, we extended this so it became

82:49worldwide.

82:51We have standard organizations that are active in currently

82:5715 to 20 countries, and another 15 to 20 are sort of looking

83:06and voting based on the rest of the work on it.

83:11And we meet three times a year.

83:14Next week, I'll be in Cologne, Germany, spending a week doing

83:20standardization, and we'll vote out the committee draft of C++20,

83:25which goes to the National Standards Committees for comments

83:30and requests for changes and improvements.

83:35Then we do that, and there's a second set of votes where

83:38hopefully everybody votes in favor.

83:40This has happened several times.

83:42The first time we finished, we started in the first technical

83:47meeting was in 1990.

83:50The last was in '98.

83:53We voted it out.

83:54That was the standard that people used till '11, or a little

83:58bit past '11.

83:59And it was an international standard.

84:02All the countries voted in favor.

84:05It took longer with '11, and I'll mention why, but all the

84:12nations voted in favor.

84:15And we work on the basis of consensus.

84:19That is, we do not want something that passes 60/40,

84:23because then we're getting dial-ins and opponents and people

84:28will complain too much.

84:30They won't complain too much, but basically it has no real

84:34effect.

84:35The standards have been obeyed.

84:37They have been working to make it easier to use many compilers,

84:43many computers, and all of that kind of stuff.

84:45And so the first, it was traditional with ISO standards to

84:51take 10 years.

84:51We did the first one in '08, brilliant.

84:54And we thought we were going to do the next one in '06, because

84:58now we are good at it.

84:59Right.

85:01It took 13.

85:03Yeah, it was named OX.

85:06It was named OX.

85:08Hoping that you would at least get it within the odds, the

85:12single digits.

85:13I thought we would get, I thought we would get six, seven,

85:17or eight.

85:17The confidence of youth.

85:19Yeah, that's right.

85:20Well, the point is that this was sort of like a second system

85:24effect.

85:25That is, we now knew how to do it.

85:27And so we're going to do it much better.

85:29And we got more ambitious.

85:30Ambitious.

85:31And it took longer.

85:32Furthermore, there is this tendency, because it's a 10-year

85:37cycle, or eight, doesn't matter.

85:41Just before you're about to ship, somebody has a bright

85:46idea.

85:46Yeah.

85:49And so we really, really must get that in.

85:53We did that successfully with the STL.

85:58Yeah.

85:59We got the standard library that gives us all the STL stuff.

86:04That basically, I think it saved C++.

86:07It was beautiful.

86:07Yeah.

86:08And then people tried it with other things, and it didn't

86:12work so well.

86:13They got things in, but it wasn't as dramatic.

86:16And it took longer and longer and longer.

86:18So after C++ 11, which was a huge improvement, and basically

86:26what most people are using today, we decided never again.

86:31And so how do you avoid those slips?

86:36And the answer is that you ship more often.

86:39So that if you have a slip on a 10-year cycle, by the time

86:47you know it's a slip, there's 11 years till you get it.

86:49Now, with a three-year cycle, there is about four years

86:55till you get it.

86:56The delay between feature freeze and shipping.

87:02So you always get one or two years more.

87:05And so we shipped 14 on time.

87:09We shipped 17 on time.

87:11And we will ship 20 on time.

87:14It'll happen.

87:18And furthermore, this gives a predictability that allows

87:23the implementers, the compiler implementers, the library

87:26implementers, they have a target, and they deliver on it.

87:3011 took two years before most compilers were good enough.

87:3514, most compilers were actually getting pretty good in 14.

87:4217, everybody shipped in 17.

87:45We are going to have at least almost everybody ship almost

87:51everything in 20.

87:53And I know this because they're shipping in 19.

87:57Predictability is good.

87:59Delivery on time is good.

88:01And so, yeah.

88:02That's great.

88:02So that's how it works.

88:03There's a lot of features that came in in C++11.

88:09There's a lot of features at the birth of C++ that were

88:13amazing and ideas with concepts in 2020.

88:17What to you is the most, just to you personally, beautiful

§07Transcript · 1:28:25–1:40:30

88:25or just you sit back and think, wow, that's just a nice and

88:33clean feature of C++?

88:35I have written two papers for the History of Programming

88:41Languages Conference, which basically ask me such questions.

88:45And I'm writing a third one, which I will deliver at the

88:50History of Programming Languages Conference in London

88:52next year.

88:54So I've been thinking about that.

88:55And there is one clear answer.

88:57Constructors and destructors.

88:59The way a constructor can establish the environment for

89:04the use of a type for an object and the destructor that

89:09cleans up any messes at the end of it.

89:12That is the key to C++.

89:14That's why we don't have to use garbage collection.

89:18That's how we can get predictable performance.

89:21That's how you can get the minimal overhead in many, many

89:25cases and have really clean types.

89:28It's the idea of constructor destructor pairs.

89:34Sometimes it comes out under the name RAII, resource

89:40acquisition is initialization, which is the idea that you

89:43grab resources and the constructor and release them

89:46and destructor.

89:47It's also the best example of why I shouldn't be an

89:51advertising.

89:51I get the best idea and I call it resource acquisition is

89:56initialization.

89:57Not the greatest naming I've ever heard.

90:01So it's types, abstraction of types.

90:08You said, I want to create my own types.

90:13So types is an essential part of C++ and making them

90:17efficient is the key part.

90:20And to you, this is almost getting philosophical, but the

90:26construction and the destruction, the creation of an

90:30instance of a type and the freeing of resources from that

90:35instance of a type is what defines the object.

90:39That's almost like birth and death is what defines human

90:45life.

90:46Yeah, that's right.

90:47By the way, philosophy is important.

90:50You can't do good language design without philosophy

90:54because what you are determining is what people can

90:57express and how.

90:58This is very important.

91:00By the way, constructors destructors came into C++ in

91:05'79, in about the second week of my work with what was then

91:11called C with classes.

91:12It is a fundamental idea.

91:15Next comes the fact that you need to control copying,

91:18because once you control, as you said, birth and death,

91:22you have to control taking copies, which is another way

91:27of creating an object.

91:28And finally, you have to be able to move things around.

91:32So you get the move operations.

91:35And that's the set of key operations you can define on a

91:39C++ type.

91:41And so to you, those things are just a beautiful part of C++

91:49that is at the core of it all.

91:51Yes.

91:51You mentioned that you hope there will be one unified set

91:55of guidelines in the future for how to construct a programming

91:59language.

91:59So perhaps not one programming language, but a unification

92:05of how we build programming languages.

92:07If you remember such statements.

92:10I have some trouble remembering it, but I know the origin

92:13of that idea.

92:14So maybe you can talk about sort of C++ has been improving.

92:18There's been a lot of programming language.

92:20Do you, where does the arc of history taking us?

92:24Do you hope that there is a unification about the languages

92:27with which we communicate in the digital space?

92:30Well, I think that languages should be designed not by

92:39clobbering language features together and doing slightly

92:44different versions of somebody else's ideas.

92:47But through the creation of a set of principles, rules of

92:54thumbs, whatever you call them.

92:56I made them for C++.

92:58And we're trying to teach people in the standards committee

93:04about these rules because a lot of people come in and say,

93:07I've got a great idea.

93:08Let's put it in the language.

93:09And then you have to ask, why does it fit in the language?

93:13Why does it fit in this language?

93:15It may fit in another language and not here, or it may fit

93:19here and not the other language.

93:21So you have to work from a set of principles and you have

93:24to develop that set of principles.

93:26And one example that I sometimes remember is I was sitting

93:35down with some of the designers of Common Lisp and we were

93:41talking about languages and language features.

93:45And obviously we didn't agree about anything because, well,

93:50Lisp is not C++ and vice versa.

93:53It's too many parentheses.

93:54But suddenly we started making progress.

93:58I said, I had this problem and I developed it.

94:05According to these ideas and this, why?

94:07We had that problem, different problem, and we develop it

94:10with the same kind of principles.

94:12And so we worked through large chunks of C++ and large

94:19chunks of Common Lisp and figured out we actually had

94:23similar sets of principles of how to do it.

94:27But the constraints on our designs were very different.

94:32And the aims for the usage was very different.

94:35But there was commonality in the way you reason about

94:41language features and the fundamental principles you

94:45are trying to do.

94:45- So do you think that's possible to, so just like there

94:50is perhaps a unified theory of physics, of the fundamental

94:55forces of physics, I'm sure there is commonalities among

95:01the languages, but there's also people involved that help

95:05drive the development of these languages.

95:07Do you have a hope or an optimism that there will be

95:13a unification?

95:14If you think about physics and Einstein towards a

95:18simplified language, do you think that's possible?

95:22- Let's remember sort of modern physics, I think, started

95:28with Galileo in the 1300s.

95:31So they've had 700 years to get going.

95:34Modern computing started in about '49.

95:39We've got, what is that, 70 years.

95:43They have 10 times.

95:45And furthermore, they are not as bothered with people

95:50using physics the way we are worried about programming

95:55is done by humans.

95:57So each have problems and constraints, the others have,

96:02but we are very immature compared to physics.

96:04So I would look at sort of the philosophical level and look

96:12for fundamental principles, like you don't leak resources,

96:18you shouldn't.

96:18You don't take errors at runtime that you don't need to.

96:26You don't violate some kind of type system.

96:30There's many kinds of type systems, but when you have one,

96:33you don't break it, et cetera, et cetera.

96:36There will be quite a few, and it will not be the same

96:42for all languages.

96:43But I think if we step back at some kind of philosophical

96:47level, we would be able to agree on sets of principles

96:52that applied to sets of problem areas.

96:56And within an area of use, like in C++'s case,

97:04what used to be called systems programming,

97:07the area between the hardware and the fluffier parts

97:12of the system, you might very well see a convergence.

97:17So these days you see Rust having adopted RAII,

97:22and sometime accuses me for having borrowed it 20 years

97:25before they discovered it.

97:27But we're seeing some kind of convergent here,

97:34instead of relying on garbage collection all the time.

97:38The garbage collection languages are doing things

97:41like the dispose patterns and such that imitates

97:47some of the construction destruction stuff.

97:50And they're trying not to use the garbage collection

97:52all the time, things like that.

97:55So there's a conversion.

97:57But I think we have to step back to the philosophical level,

98:00agree on principles, and then we'll see some convergences.

98:05And it will be application domain specific.

98:10- So a crazy question, but I work a lot with machine

98:16learning, with deep learning.

98:17I'm not sure if you touch that world much,

98:19but you could think of programming as a thing

98:24that takes some input.

98:25Programming is the task of creating a program,

98:28and a program takes some input and produces some output.

98:31So machine learning systems train on data

98:35in order to be able to take an input and produce output.

98:39But there are messy, fuzzy things, much like

98:46we as children grow up.

98:48You know, we take some input, we make some output,

98:51but we're noisy, we mess up a lot.

98:53We're definitely not reliable.

98:54Biological systems are a giant mess.

98:56So there's a sense in which machine learning

99:01is a kind of way of programming, but just fuzzy.

99:04It's very, very, very different than C++.

99:08Because C++ is, just like you said,

99:12it's extremely reliable, it's efficient,

99:16it's, you know, you can measure, you can test

99:19in a bunch of different ways.

99:20With biological systems or machine learning systems,

99:24you can't say much except sort of empirically

99:28saying that 99.8% of the time it seems to work.

99:31What do you think about this fuzzy kind of programming?

99:36Do you even see it as programming?

99:39Is it totally another kind of world?

99:42- I think it's a different kind of world.

99:46And it is fuzzy.

99:47And in my domain, I don't like fuzziness.

99:50That is, people say things like

99:55they want everybody to be able to program.

99:57But I don't want everybody to program

100:01my airplane controls or the car controls.

100:07I want that to be done by engineers.

100:09I want that to be done with people

100:12that are specifically educated and trained.

100:15For doing, building things.

100:19And it is not for everybody.

100:21Similarly, a language like C++ is not for everybody.

100:26It is generated to be a sharp and effective tool

100:30for professionals, basically.

§08Transcript · 1:40:34–1:47:05

100:34And definitely for people who aim

100:38at some kind of precision.

100:40You don't have people doing calculations

100:43without understanding math.

100:45Counting on your fingers is not going to cut it

100:48if you want to fly to the moon.

100:50And so there are areas where an 84% accuracy rate,

101:0016% false positive rate is perfectly acceptable.

101:06And where people will probably get no more than 70.

101:11You said 98%.

101:12What I have seen is more like 84.

101:16And by really a lot of blood, sweat, and tears,

101:19you can get up to 92 and a half.

101:21So this is fine if it is, say, pre-screening stuff

101:29before the human look at it.

101:31It is not good enough for life-threatening situations.

101:37And so there are lots of areas where the fuzziness

101:41is perfectly acceptable and good and better than humans,

101:45cheaper than humans.

101:46But it is not the kind of engineering stuff

101:49I am mostly interested in.

101:52I worry a bit about machine learning

101:56in the context of cars.

101:58You know much more about this than I do.

102:00- I worry too.

102:00- But I am sort of an amateur here.

102:04I have read some of the papers,

102:05but I have not ever done it.

102:07And the idea that scares me the most

102:12is the one I have heard,

102:15and I do not know how common it is,

102:17that you have this AI system, machine learning,

102:25all of these trained neural nets.

102:28And when there is something that is too complicated,

102:33they ask the human for help.

102:36But the human is reading a book or asleep,

102:39and he has 30 seconds or three seconds

102:44to figure out what the problem was

102:46that the AI system could not handle

102:48and do the right thing.

102:49This is scary.

102:50I mean, how do you do the cuddle

102:54between the machine and the human?

102:56- It is very, very difficult.

102:59And for the designer of one of the most recent

103:04designer of one of the most reliable, efficient,

103:06and powerful programming languages, C++,

103:08I can understand why that world is actually unappealing.

103:15It is for most engineers.

103:16To me, it is extremely appealing

103:18because we do not know how to get that interaction right.

103:22But I think it is possible.

103:24But it is very, very hard.

103:25- It is.

103:25- And--

103:26- I was stating a problem, not a solution.

103:28- Yes, that is impossible.

103:30- I mean--

103:30- I would much rather never rely on the human.

103:33If you are driving a nuclear reactor,

103:35or an autonomous vehicle,

103:37it is much better to design systems written in C++

103:42that never ask human for help.

103:44- Let us just get one fact in.

103:47- Yes.

103:47- All of this AI stuff is on top of C++.

103:51(laughing)

103:53So that is one reason I have to keep a weather eye out

103:58on what is going on in that field.

103:59But I will never become an expert in that area.

104:02But it is a good example of how you separate

104:04different areas of applications

104:08and you have to have different tools,

104:10different principles, and they interact.

104:14No major system today is written in one language.

104:17And there are good reasons for that.

104:19- When you look back at your life work,

104:24what is a moment, what is an event,

104:30creation that you are really proud of?

104:33That you say, "Damn, I did pretty good there."

104:35Is it as obvious as the creation of C++?

104:40- It is obvious.

104:41I have spent a lot of time with C++

104:44and it is a combination of a few good ideas,

104:49a lot of hard work, and a bit of luck.

104:51And I have tried to get away from it a few times,

104:55but I get dragged in again,

104:57partly because I am most effective in this area.

105:00And partly because what I do has much more impact

105:05if I do it in the context of C++.

105:08I have four and a half million people

105:11that pick it up tomorrow if I get something right.

105:14If I did it in another field,

105:16I would have to start learning,

105:17then I have to build it,

105:18and then we'll see if anybody wants to use it.

105:20One of the things that has kept me going

105:26for all of these years is,

105:27one, the good things that people do with it.

105:30And the interesting things they do with it.

105:34And also, I get to see a lot of interesting stuff

105:39and talk to a lot of interesting people.

105:41I mean, if it has just been statements on paper,

105:47on a screen,

105:48I don't think I could have kept going.

105:51But I get to see the telescopes up on Mount Akia,

105:55and I actually went and see how Ford built cars,

105:59and I got to JPL and see how they do the Mars rovers.

106:06There's so much cool stuff going on,

106:09and most of the cool stuff is done by pretty nice people.

106:12And sometimes in very nice places,

106:14Cambridge, Sofia Antipolis, Silicon Valley.

106:21There's more to it than just code,

106:26but code is central.

106:27- On top of the code are the people in very nice places.

106:32Well, I think I speak for millions of people,

106:35Bjarn, in saying thank you for creating this language

106:40that so many systems are built on top of

106:43that make a better world.

106:46So thank you.

106:47And thank you for talking today.

106:49- Yeah, thanks. - I really appreciate it.

106:50- And we'll make it even better.

106:51- Good.

106:52(upbeat music)

106:55(upbeat music)

106:57(upbeat music)

107:00(upbeat music)

107:03(upbeat music)

107:05(upbeat music)

— 读到这里,多懂一点。 —