CppCon 2019: Bjarne Stroustrup “C++20: C++ at 40”

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I really like his presentations. Its almost like he created the language! Thanks for the link

👍︎︎ 74 👤︎︎ u/viktor4130 📅︎︎ Sep 17 2019 🗫︎ replies

Wait, is it CppCon already? Nice, totally missed that.

👍︎︎ 26 👤︎︎ u/SirToxe 📅︎︎ Sep 17 2019 🗫︎ replies

Why did they disable the comments? There were some interesting discussions!

👍︎︎ 25 👤︎︎ u/emdeka87 📅︎︎ Sep 17 2019 🗫︎ replies

I liked his analogy of an "average c++ program is like an onion", but I can't relate. The average c++ programs I've worked on have all been a spaghetti mess.

👍︎︎ 9 👤︎︎ u/software-dev-in-rsa 📅︎︎ Sep 18 2019 🗫︎ replies

That dude looks like he could create a programming language himself 🤡

👍︎︎ 13 👤︎︎ u/Behelitoh 📅︎︎ Sep 17 2019 🗫︎ replies

The C++ creator referred to Raspberry Pi many times in his talk, which I've just begun to have my hands on: an amazing world!

👍︎︎ 2 👤︎︎ u/g4x86 📅︎︎ Sep 18 2019 🗫︎ replies

I like how most of the comments talk about why comments are disabled but not about C++. Can we talk about C++ for a change? At 44:30 there is a slide with overloaded() trick for use with std::visit.

IMO the entire slide is completely unreadable slash unmaintainable when there are a lot of such things in the code. Mr. Stroustrup, could you please come up with a good maintainable abstraction instead of overloaded() in C++23? Pattern matching of some sorts would be really nice to see.

👍︎︎ 1 👤︎︎ u/neuroblaster 📅︎︎ Sep 24 2019 🗫︎ replies
Captions
[Applause] so I would like to introduce you to a friend of mine April can you come up here so our keynote today is is a look at C++ at 40 years so there's some looking back but most of its about looking forward to the future and so as I think about the future C++ I think about young people good morning April good morning so April is one of two high school students who are registered for this conference and that is a person for us and I'm very excited to have her here you how do you use C++ April I work on a lot of small hobby projects like currently I'm just making chess but I love experimenting especially with games and huh okay that's exciting all right would you be interested in what the creator of C++ has to say about the future of C++ definitely then why didn't you ask this group to welcome our keynote speaker okay everybody please give a warm soup applause for the keynote speaker inventor of C++ we are nice to stuff [Applause] so welcome and it's quite a crowd that is not a picture of the global C++ community it could have been but it's just a slide I borrowed from the web of something I'm going to talk about C++ 20 not the the fartist and future because I'm not actually that great is a sort of a blue sky visionary I'll talk about things that we know a little bit about as a matter of fact we know almost everything about which is c plus plus 20 and then I'll try and put it a little bit into a perspective because it is about 40 years across - a month before I started that when I started at this so it's it's an obvious topic I will however speak mostly about the the current in the near future I'm not going to go on about how it really was in the old days and you should have been there and all of that stuff there there's a sort of one way of contrasting what has happened in those 40 years the two gadgets are more or less to scale you can see the handset and the phone there and I will point out that the gadgets to the left the old one is something that has to be attached to a physical system inside a war for its power and connectivity it's it's it's very different today and one of the points I will make possibly repeatedly this at C++ actually had a significant part of this transition if you look at a gadget like the one with C++ inside it's not just that it's running C++ programs and that the key communication systems are C++ it's also that the manufacturing processes the sign of the processes the data baba has C++ in it I have another talk which I won't give today which is known as the invisible C++ the point is that if you do it right nobody can see it but it is everywhere and I'll go look here is that's actually the computer I started off so pdp-11 7c the two guys of course is Ken and Dennis this year is the 50th anniversary of Unix which is another great thing that has taken over the world and is found everywhere I'd like to point out that that computer there is less than a thousands of the speed of a Raspberry Pi it cost maybe a thousand times more than a Raspberry Pi it the Raspberry Pi weighs about 10,000 times less but then of course that computer only had to serve 40 researchers the world has changed and we've been part of that change one problem I have and I try to address is that people actually describe C++ in ways that harms the community and it harms the effectiveness of what we do they they they have ideas that were outdated in 1990 and we hear them all the time there can never be an r2 got anything I say on the web without somebody coming making noises that were inappropriate in 85 or thereabout but I'll point out that what is old is not necessarily bad what is new is not necessarily good we have to look at C++ as it is today and by today I mean C++ 20 I'll show a lot of code examples I'm not going to label them with dates you can have fun to think about when you could do this and whether you can actually do it on your current compiler and whether you can do it in your codebase I actually expect and somewhat hope that the answer is often you can't and therefore there is a path forward and we are getting ready to get you there and it's not a talk about details this is a keynote I think every slide I have there will be a one hour or more tour digging into that in depth I'm not going to try find the talk and listen to it and my general approach both for using C++ and for teaching it is to focus on the essentials try and figure out what is the essentials focus on that and be advanced and clever only when you absolutely must there's no prize for using the largest number of features or showing the most sophisticated example that's something you do when you must you tried to keep symbol and we also since C++ is an older language have to distinguish between what is good what is effective well what is useful and what's legal I'm not a language lawyer well I guess I am but I don't want to be and I want to sort of ignore the the subtle details as often as I can and I'm going to going to do here that means that we need to have guidelines for how to use the language well and I'm working on the C++ core guidelines which can be found in in many places this is not unique to C++ but I think it's very important part of the inspiration for this talk and part of the the background is an experiment I do every year I I get a class at Columbia and I get in students finally undergrads first-year graduates from all over the world all over the US all over the world and so I give them that book there and the first exercise is tell me what you didn't know about the language that is you read the language chapters and you tell me what you didn't know then second one do the same for the chapters about the library there's 200 pages 2 weeks work and I get a lot of feedback and I'm always shocked and so some of the things I'm going to tell you here you know very well but assume that the average student coming out of good university will have missed about half and that's why I think this is a worthwhile exercise C++ I think is principled and eclectic it's a general-purpose programming language and the key to the good use is definition implementation and use of lightweight abstractions and a lightweight abstraction is something that directly represent an idea and does it in an affordable and simple manner and a language is not just product development there's people says let's let's just build the next release you can't just build the next release for something that's supposed to last decades you have to have a design philosophy and something to help you find what is essential and I think we have had some success in that and this has given a certain degree of stability over the decades and a certain flexibility that you don't get if you just focus on the individual feature I mean people who think that they can't write a program without a virtual function because then it isn't good and have been disproven a few times over the years on the other hand there are good uses of just about every feature and C++ and one of the things that have been a focus and also was reflected in John's opening remarks is that in the design of C++ the view is we have to do whatever it takes to get good production code C++ is a language for building things for delivering systems maintaining systems making systems affordable it's it's not particularly aimed at a particular little group academics students etc it's it's it's it's ferret II and our purpose and I'm sometimes reminded about manager high level manager once MIT in a company which shall remain unnamed who said yeah yeah we don't really like C++ we only use it because it's the only thing that works I thought a couple of seconds and said oh thank you so but you can't just be expert friendly I was actually the one that coined the phrase expert friendly as a caricature to people who wanted to be just expert friendly you have to be simply enough for casual use most programmers as long as our use of C++ grows are going to be relative novices and we have to focus on making the language simple enough and the techniques for using it's simple enough that people can come on board we should not try to enforce some kind of idea of theoretical purity the series changed over the years for starters and I've never seen a philosophy that covered absolutely everything that everybody needed in the C++ community it's eclectic they're principled so that's what we're going to do so high-level aims C++ is evolutionary I knew from day one that I couldn't build a complete language to serving everything that I would like to sir therefore you have to start at the beginning and then grow based on feedback based on what you learn so you have to evolve and you have to support gradual adoption because well people can't just throw all of their code away and start again from scratch not large organizations not people millions of lines of billions of lines of code deployed and furthermore you have to be stable a lot of the things I like most about C++ and systems building are things that you can't actually replace that easily when you put a lot of gadgets out and in the wild you can't just go and replace them when you build a large infrastructure it has to keep running you can't take the electricity supply down for a month while you upgrade the software controls this just doesn't work so you have to have stability compatibility or years and decades people tend to forget that and think let's just solve the urgent problems now but you know what happens when you succeed your stuff lasts only failures will you you can you can just redo them again in a year or two successes lasts and there's been other things try to make simple things simple which is the thing I'm going to talk about in this talk and the zero hour principle that is don't have distributed fat try and make sure that you can write the code so that you can afford to run it and you have to aim high I don't actually think a 10% improvement is such a great thing in the big picture it is a great thing for a particular system for a particular year but if you are thinking in terms of decades and language designed and community what you actually have to go for is significantly change the way we design implement software basically we have to change the way we think and and I'm not being patronizing and says you have to change the way you think its way including me okay so the aim and the purpose of the exercise is to create really good applications it's really good users and as usual I have some pictures because I can't talk about all of these applications though sometimes I would like to so the purpose of the exercise is to have great applications and you know looking back we actually succeeded to a large extent this this should be a happy event because you know you did something good over the years in the area of programming and design code is written deployed and maintained in ways that are radically different from what it was a few decades ago and C++ has been part of that the C++ community has been part of that the the use of abstraction to more directly express ideas has has spread it's not just C++ its simplest was has been one of the drivers of evolution of languages in the poor some people wanted to do things better than C++ some things wanted to do things different from C++ but it's been one of the drivers hardware have improved but how do I can only improve in combination with software these days software and hardware are not totally disjoint and C prosper has been part of that and our compiler infrastructure the the way we analyze our code and generate code is radically different from what it was and I'm happy to see that C++ actually through LLVM and that kind of stuff is actually one of the things that helps most programming languages modern programming languages these days and then of course the applications the scale is just enormous ly different from what it was sophistication in mostly difference from that C++ has had a major impact in in science and engineering I mean there's this there's a reason I've gotten highest awards for engineering and such we we've done well okay so yeah there's been change C++ has been been key to some of those dramatic changes so let's get back to code if you look at the design and evolution of C++ you'll find a list of rules of thumb I I felt shy I wouldn't call them principles so rules are thumb and this list here which is a subset of that list is going to be my guide for this talk and there's a few you have probably seen most of them in other words most of you are seen most of them it's not just a list of features it's a evolving language the trouble and the challenge is to maintain coherency for a long long time and aim for gradual and steady improvement and there was and there is a plan and you can read up on it there's some links there so let's look at it C++ is static type system will equal support for building and user defined type that's the idea and become pretty close in other words the type system is extensible and having a static type system allows us to catch things at compile time because we can express things so the compiler can understand it one of the things I like best about that is that if I can have my errors caught at compile time I don't have to have a runtime error handler for that kind of error and one of the hardest pieces of code to write is error handling strange things happen and you have to deal with them and when things are strange they're hard to deal with performance comes to a large extent by allowing the compilers and optimizes more information about what you're doing again we're improving there and the root of a lot of the best improvements are in the static type system and then the static type system allows us to be flexible to be expressive we have overloading when we started back in the dark ages square root of tool gave a crash because early c couldn't figure out that int could be used as a double because it required a conversion we have as type system that allows us to express things directly and make it work we get generic programming metaprogramming and a lot of compile time evaluation if we can move computation to compile time we can catch errors and run faster so let's let's look at it one of the things that C++ has a lot of languages are not focused on that is actually support both value and reference semantics so basically simple things like this X becomes Y plus said X can be an int complex matrix and basically the semantics is what you would expect X becomes Y then well X has the same value of y but they're still independent objects on the other hand we have a lot of types that must be sort of pointer reference like it refers to something over there and so we can assign through them read from them and if we make something that has a point of reference immensities we have an assignment there they'll refer to the same object the combination of the two is is one of the keys to C++ a success and so on our most comments hi our value types integers character strings the containers and I think the semantics is ideal but pointers and references are essential in the implementation of some of the most interesting of those types so we have ordinary pointers references unique pointers share pointers all the rest and basically when we want to pass something along we don't always want to do a copy we just want to refer to it and so they need to it and and both are needed to give good use of the hardware resources so basically once you get the ability to to to have you building types and user-defined types work uniformly you don't have to think all the time about what it is the example here is of course the classical vector example which by the way shows two levels of abstraction the vector level and the implementation which is pointers and stuff but I can have vectors or building types vectors of user-defined types and this all works recursively so I can have a vector of vector of integers and that kind of stuff if there have been different rules for building types and user-defined types say user-defined types has to be created with you and leave them the FreeStore and building types can be on the stack this kind of stuff would not have worked well so one of the things that that has driven C++ is direct use of the machine c++ usually not necessarily but usually don't work on a virtual machine and it's a it's not a mathematical abstraction it's it's an abstraction of the machine and well it means that the objects are lying in memory and we can point to them memory is a sequence of object or rather it is a set of sequences of objects and we can combine things into new types just by sticking values next to each other object next to each other you get a raise if they're the same type if there are different types you can create objects structures classes and the pointers they are turns into some kind of things with with reference matrix this is a very simple abstraction of hardware this is the beauty of Dennis Rich's view of the machine and the design of C and I mention this because at least a third of my class when I do this class don't get it they haven't seen this before this is shocking to them and so please don't forget when you're introducing new people to C++ if they if they haven't seen something like C before they might need to see this slide and of course again we abstract we go one level higher when you're dealing with with hardware directly all the sizes are fixed there's no flexibility there's no protection there's no elegant error detection so we move up a level you always move up a level when we can so we can have bit sets that will give us the bit operations but for an arbitrary number of elements and we have arrays which have have their size known and we have spans so there I have an array of some bytes and I would like to manipulate it this is something happens all the time think buffers think networking think string manipulation character manipulation and such and so I want to spend over that so that I can pass it around I can iterate over it with range for loops and and this is fine notice that I don't actually have to mention the type of the elements in the span or the size of the the number of elements in that span because well a compiler knew it already and why should I tell the combine something that compiler knows I can if I want to give a size I can if I want to give a type sometimes we need this but whenever I say something I might get it wrong the compiler is more likely with almost perfect knowledge and very systematic rules are more likely to get it right on average than I am so I don't actually trust this kind of stuff where I am being explicit as much as when the compiler knows it again we are moving up a level from the pointers to zero terminated her arrays of integer characters or something like that and this is something I tend to refer to as the onion principle the average C++ program is is a set of layers of abstraction and you can always peel off one layer you get more details to express you get more you get more controls you get more opportunities to make mistakes you have to say more to work in other words you cry more and basically this is one of the ways we manage complexity we have many many layers and once I found the picture I noticed something interesting this is not just layers there are sort of irregularities that are being absorbed and encapsulated this is good stuff so I think it's a it's a good way of thinking about how to structure software so let's get back a bit one of the first things that came into C++ was was basically constructors and destructors and so basically we can look at the constructor destructor pairs they came in in the second week of C++ design so there they are the rock bottom of C++ here everything rest on it so we have a gadget we create a gadget it acquires some resources or needs to function memories file handles locks I don't know and no most of the time I don't have to know and I don't have to care because once we have finished it cranes are bits own miss this is important we can copy move them rest give it a good user interface and then the next level of abstractions down here there are is of course represented in terms of something else this is fine and if we have a local variable like this that's a gadget if I throw an exception if I return from the middle if I fall down the bottom I know that whatever misses that might be will be cleaned up so basically if you have every resource owned by an owner like gadget then the cleanup the destruction happens automatically you don't have to worry about it it also tends to be optimally fast it when you get out of the scope it goes away means you have minimal resource retention reasonable retention is a real pain in the neck if you have limited resources and basically don't don't don't try oops whoops don't try and handle ownership with overall pointers we have maybe fifty years of experience to say we are not very good at it we actually need to do something else and the pros do unique facility in C++ is that it is directly supported there's one thing that has been a source of a lot of user pointers for ownership which is people wants to make a big thing like a factory function and so they create an object they fill it up with good stuff and have to pass it back and the traditional way is to pass it back with a pointer if you do that you now have to deal with memory management explicitly you have to remember to delete that pointer all you have to use the share pointer which means that you have said maintain use counts and synchronization on use counts or you have to use unique pointers which a lot of people haven't learned there's a much better way it's over the mountains but we can do it better now so here we simply return the gadget and most of the time the compiler is smart enough to figure out that I have a local variable there which I want to become a local variable in the external scope very soon so let's allocate it so that we don't have to move it at all we certainly don't want to copy it but if the compiler can't figure this out we fall back - move semantics where he says okay there's the gadget big things tend to be a handle to the real stuff handles are really cheap to copy so what we do we steal a representation from G put it into G C into G G and remove the link from G problem solved we can move something with a million elements to file handles and and the lock out at the cost of essentially nothing so that's important and now we have a complete control of the of the object cycle of the life side of objects creation copy move and destruction that's important and this is sort of pervasive in the standard library I wish it was pervasive in all libraries but we're getting there and it's not just memory so when people talk about garbage collection they can handle everything up here provided it's only memory but there's lots and lots of things that aren't memory threads mutexes scope locks file handles Teradata so I'm not a great fan of garbage collection I would like to put it out of business if I can and of course again all of this works recursively so I can have a vector of a forward list of a pair of a string and a J thread Chara it it things has to work in composition in collaboration this is how we get reasonably simple maintainable code so back to this list of rules support conversation of software from separately developed parts oh yeah C++ applications are not all sort of grand and sexy that's a coffee machine there it's it's a course programmed in C++ I talk to the people who are doing we development of it and I like this one because my favorite definition of a programmer is a machine for turning caffeine into code and yes I have a machine not quite as fancy at that in my office otherwise I'll never get any code done anyway back to composition we compose software out of modules remember I'm assuming C++ 20 here and so if I want to write a map printer it's just sort of the smallest reasonable program that fits on the slide I'll import STD which unfortunately isn't standard but it's easy enough to define you just grab all of the standard library and then I import my own containers and then I want to export the the map printer there it's it's Tim template it requires takes a forward range and prints anything that has printable key and value types and then I do a loop over the elements there using structured binding and write out the key value pair that's nice it's simple and as a matter of fact it's you don't have to remember a lot of things just give me the standard library that that's the way to do it so basically modules support clean code it minimizes dependencies about things you bring in and it avoids circular dependencies some of you are probably thinking well that's not the way my code looks I have oodles of includes and we have to be careful about them yeah that's the point you have far too many intrudes and you have to be far too careful we're doing better these days so much energy if I import a and B means the same as importing P na hi that's the way things should be right why should I know about this and why should a be able to in in fact be with information if I wanted dependencies I should state them and only the use part of an important module has turned into generated code so when I grab all of a CD there I didn't load up my memory with etc junk that I didn't use I just said I I want it to be accessible and there's only one copy of a module so you don't have to copy this so to analyze it a hundred times because you included something a hundred times so basically all the major features of C++ exists to support composition modules classes concepts templates functions aliases if you can't compose you have to replicate and you get the code many times if you get the code many times if you fix the bug in one place you can't fix it in the other places that is no good you want to actually compose things out of unique parts incidentally that is a Lego Turing machine given that you can do anything but it is the lowest level of abstraction so you may not want to do that in production code so let's see generic programming is something that I worked on on and off the first paper on serial classes the ancestor of the C++ which was written in 81 says we need generic programming and I thought I could do it with macros boy was I wrong generic programming we want to write code that works for types that meet abstract inquire requirements so we start our to be able to sort anything that sort of or do arithmetic on anything that actually is our numbers and things like that so we have concepts like forward iterator is in integral is irregular and can be sorted things like that you'll notice the integral is in K and R 1 so this is pretty fundamental and or not all that new and a concept is a simple compile time predicate on a set of types and values so if I won't say a sortable range which is the things that I want this all to work on I can say it should be an access range it should be its iterator should be permeable and it should have an indirect strict will order you can look the meaning of those concepts up in the C++ 20 committee draft that is now being voted on and this is good good standard stuff and now I can say I want to sort a sortable range say that's a vector of strings that'll work fine because the string has those properties vector of strings has those properties I can try it with a list of integers however lists do not have the random access range so it does not work so static type checking works out nicely the ability to express things concisely by using what the compiler already knows works nicely I have been wondering the right sort of Vic in standard C++ for ages I've had to fake it so far we do better but anyway what if I really wanted to sort the list well I can define a forwards audible range which basically is just a forward range and it has iterators that can be used for sorting against standard and now I define my own forward sortable range there's a nice algorithm for that in in elements of programming or you can just copy the range into a vector sort it and copy it back again but you can do it and now they were both work and if I saw the list well I can't sort it with with a sortable range which is random access so we'll pick that one and for the vector it has all the properties required for this so it'll work it just works out of the box and so my idea is that genetic programming is just programming it's programming like we've known it for a long time take your time machine go back to last year listen to my keynote then it was all about that basically a concept specifies an interface a type specifies an interface plus a layout and basically is not that much different between say saw the vector and square root of x Bay both of them works for a set of types that has a set of required properties and it doesn't well and I don't actually really want to know whether that was an overloaded set of functions and that's a generic function it'll just work from a user point of view and of course the default sort uses less than and we have all the alternatives for specifying what criteria we want I can sort in in the opposite order I can sort the lower case if it's if it's lists and such so let's breeze through this again object-oriented programming has had a bad press for the last few years I guess we can blame various languages including C++ but certainly there are languages the things that you have to be using heritance all the time C++ can do inheritance and there's a lot of applications where it's the right thing here's a with draw shapes examples from about 69 67 there abouts when I first learned it from Christ new goal or design Simula and came up with this stuff we have an abstract base class we have some overriding from actions we build a hierarchy and then we can draw the shapes here I've taken the forward range I want all the things to be derived from shape then I can do a range for loop over and print them all out the test example here shows that I make a bunch of shear pointers you need pointers to do physical object oriented programming because you program against interfaces you don't know what I in those interfaces you don't know if that shape is a circle or a smiley face or triangle so you inherently work with less information through pointers and I use more smart pointers of course because in this case I need pointers and I use the smart pointers to avoid leaks I really hate leaks now I'm just wondering is this object-oriented programming I don't need a runtime resolution in many cases and so I can use a variant give me a variant of a circle a triangle and smiley this is a closed set of alternatives this solution is an open set of alternatives but because there's a closed set of alternatives you can do certain optimizations that you couldn't do with an open set so I can do things like this draw draw all the B in that vector here which was a bit of variant and I visit them and if it's a circle to it the circle way trying to robot the usual stuff this by the way works much better if the variance if the elements of the variants are roughly the same size or you have to start implementing within directions so 54 close set of similar objects this make a lot of sense for an open set of dramatically different objects this one makes more sense this is like problem here I don't know if you noticed the overloaded function here that's the one that allows me to write the variants visitor they're quite simply with lambdas are used so you know see perhaps past when G isn't perfect it is not the end end of our development in C++ but you know C++ is extensible if it's not understand that you can just build your own here's your bill to your own I hope this one will be in the it the next standard C++ 23 this is not totally obvious to anybody who the background in old C++ or C but you can look and give it up there's always another level of expertise there's always another sort of layer of the onion you can peel off some of them are quite simple not necessarily easy to understand but you know if you need more than it's available at the simplest level at the most abstract level at the most convenient level you can always peel off a layer of Union so we need to use machines and operating systems resources simply and directly and concurrency is one of the major things I mean just about anything we do these days involve some form of concurrency and to make life difficult there are many many forms of concurrency so here I'm thinking about logging thinking about the basic level of concurrency with threads and locks so I have a musics that protects some data I have another musics that take some other data I have to grab both of them to do an operation I have to grab both mutexes so so true to get any work done and I better not deadlock I mean I don't want this piece of code to take em one before him - and this one - take him - before m1 it's simply solved in the library you ask the school block to grab both it will proceed here once it's got post and this is our AII so it releases at the end of the scope it sits so are quite simple and removes one of the most common forms of of deadlocks there's people who have been working over the last sort of twenty years they observed that mutex is an operating system resource and therefore relatively expensive so we need to protect the use of music C's with something cheaper so we get to double-locked initialization using an atomic for that and so we go through the the operations of of double-locked initialization which looks very very simple and we have eliminated the data races that could easily happen with with bad code of this and it's at a quite reasonable level of abstraction you use a cheap synchronization mechanism so what you to protect a expensive mechanism which protects the precious initialization if this is too high level for you if you really need to chew it for a particular piece of code maybe code on a machine where the compilers weren't able to generate this code from the higher-level code you can do it there's always another layer of the Audion but don't do this unless you have to so one thing that I find with C+ process that it's tunable and there's a scenario I find in person on the web many times every year a proponent for some usually new language access look see X is faster than or as fast as or almost as fast as C++ it is interesting that being not that much slower than C++ is often claimed to be a great achievement okay the answer is well the C++ su c-plus performance is very poor C++ it's it's not colloquial no experience C++ programmer would write it like that try this it is as fast as or faster than what you just showed and the proponents makes usually says that's cheating you have violated one of my fundamental assumptions about how you should write code the C++ programmer scratchies hints is this not one of my assumptions anyway before all this discussion got much further another proponent of C++ comes in and says you know the the first C++ programmers version is it's not nearly as fast as it should be here's a much faster version I've tuned it carefully and by now the proponents of language X says but my version is much easier simpler cleaner and some C++ proponents is here but I need the performance I need this more size or whatever is we needed okay but if we grant the Iligan easier safer which are properties I really love and I want them in C++ if I can get them so we wait soon after some other proponents says yeah here's a library that does that sometimes it's a bit clunky but it will do it and then you wait a bit and if it really was as important as was claimed here C++ will develop a feature that will actually do that matching the original claims of performance and elegance C++ is tunable and it is a Volvo deliberately available from day one and so since it's tunable you can make some simple things simple as long as you don't make complicated tasks to impossible or unreasonably hard to do and you go to the onion principle but please provide that first level and make it possible to go to the next level don't don't close the door though so this this layer architecture is good and as you saw for the onion it's not quite layers its encapsulated areas of code and you don't drop to the lower level of abstraction unless you really really need to after or interfaces one of the uses of interfaces is to hide ugly stuff and so it doesn't have to be pretty all the way down and don't make any claims of the need to go over level and need to optimize unless you measure and and be careful I've seen examples recently where people optimized based on their local machine their laptop or something like that and they got really nice results they proved that things really work much faster with their fine-tuned code and then you move it on to the server and the production stuff and you have actually pesum eyes the optimization I mean many more threads many more locking can really kill you if you have optimized for the wrong thing by the way the first time I noticed this phenomenon a long long time ago doesn't eh is John Bentley came and showed a highly optimized program for a pdp-11 7 see that when ported to a Cray which was a hundred times faster ran 16 times law so be careful yes never talk about performance without measure but really be careful furthermore if you have something that runs nicely on on one of the big national lab machines it may actually not work very well on your machine and so you may have to change the optimization and such when you move it onto to smaller machines and so this is one reason you want the higher-level interfaces because then you can go to the lower level separately from different kind of architectures again direct use of system resources here's a very simple use I have n threads that does my in tasks fine we use our aii at the end of the block they all wait for each other and when everybody is finished with receipt can't be much simpler than that however what if I'm doing something like a parallel search and I only want to know one so when one of these tasks straights come up and says I got it I really like to tell the other the other threads the other tasks to go away that's the way you do that with J threads you have a stop token and most of the code that we write has some kind of outer loop that says do something then do more than do more and so at a trouble loop you just check are people still interested in my result and if the answer is no I'll go away that's the older solution to this problem it is now quite simple supported by the standard again first level second level of the onion okay I really don't actually want to write threads and tasks and lock synchronization that kind of stuff what I want is parallel algorithms if I can get them I want to sort things compilers can't be trusted to actually figure out whether you want to parallel eyes and vector eyes because well that depends on the data and by and large the compilers don't know about the data so I can give it hints on sequence parallel parallel on sequence this is fine and I can write around there's a scaling function here I wanted vector eyes and I just want to scale all the elements this this is sort of the level of code I would like to write there's only one snag we ran out of time for C++ 20 so I have to drop down one level again here is what we are provided there's an execution policy like power on sake and on sick and it uses the old-style iterate iterators I really want to use my range well it's not it's not brain surgery you just write it and one thing that has been increasing from from day one C++ but especially since Gabby does raisin I proposed constant expression evaluation context for functions in eleven is compile time programming the basic idea is if you can move computation from runtime to compile time you can get more elegant code you can get more performant code you get less calculation off on the doodle pad that gives you constants that might go wrong if you can do it once by the compiler instead of a billion times you probably have a performance advantage then you don't meet the runtime error handling again and I like constants they are getting one more important with concurrency because you can't have a race condition on a constant and it's everywhere all loading and virtual functions each move a very common operation from writing it explicitly or resolving at runtime to compile time templates does a lot of that very attic templates is a good example of that that has created much more flexibility in the way we write our interfaces and basically context work in so here's the integer square root that I got a male about maybe 20 years ago somebody in the embedded systems industry wandered means as a square root at compile time and it took us a few years to figure out how to do it but that's the simple square root algorithm I know I'm saying it's crunched expa so I can take the square root of nine I could have done that myself but then I would have to match it constants three in the program and how would I know that it was meant to be the square root of nine because when I'm reading your code if I see three I do not know why it's there ELISA says so so you'd have to have a comment that is far longer and long or long-winded than the then the code nurse instead of square root of 1 2 3 4 I may not have actually have recognized at all and it's not just compile time computation the C++ type system is extensible it is meant to provide the same support for user-defined code and buildings user-defined types and building types so we can do that here's an example from the from chrono from the date library I want to write out the week day of some day in the past and if I want to check it's right I didn't actually have the compiler check it for me the static assert that means that things like June with the weekday of June 21 2016 is actually comprehensible to the compiler it's comprehended and it can figure out the answer and it does it also means that the code here well there will be no code generated but the code here will also be very close to optimal and we can use things like complex numbers 3 plus 2.7 III for imaginary composing times which is quite common and composing real strings as opposed to C style strings and it's it's again it's quite simple here's the code for the suffix I used for imaginary it simply returns a complex number with an imaginary part and Cyril as the real side so on so on it's fairly simple and elegant again back to direct use of the the hardware one of the things that's really really fundamental in the program's so fundamental that we don't usually think about it is stack frames when we call a function it creates a stack frame that uses the Machine and it uses stacks machines optimized for stacks however if you want a code she want to have a computation that gives her a salt wait a bit and when you ask it the next time you it continues its computation and give that the next research then we have to be able to do invocation frames that doesn't follow a stack doesn't necessarily follow a stack allocation strategy so would like to do that and I particularly would like to do that because it was my bread and butter for 10 years with the early years of C++ we wouldn't be here if C++ hadn't been really good at that but unfortunately Sun developed the SPARC architecture where my trickery with stack frames and registers didn't work anymore and they didn't ship it so we lost it so one of the things cold chains get directly is generators and pipelines when you get a little bit more advanced you can get fairly sophisticated simulations which was what I was doing but here's an hour just misses sealed I would like an infinite sequence starting the two I want to filter out the things that aren't crimes and I would like to get me the first 10,000 of those that's fairly simple we are wants print them we need a little bit of boilerplate here is the core routine it has it returns a generator that each time you're asked it's gives it the next integer so basically the first time you call it it gives you start which was to the second time 3 and so on and then here's the way we we take from a sequence if we have a sequence here essentially infinite we can simply give me in the next element until we have gotten the n elements we wanted the count elements we wanted 10,000 in that case the Eratosthenes a sieve is a little bit more complicated to give me the first one make a filter then then do that recursively and then the filter simply looks for the next X Prime okay it actually works this the output there I didn't hand calculate this that was all the code you needed yeah C++ in space I don't know if you noticed I wasn't actually using integers I was writing generic code very primitive generic code I had are using int being integer but what if I wanted more prior more Prime's I could use a long long simply by aliasing long long instead of it and I could use my infinite precision integer if I really wanted big Prime's this is a brute-force technique it's not the way you would probably do just illnesses see but it illustrates the techniques and it illustrates that you have the cold chains that are fundamentally a way of organizing computation and using hardware with the generic code and aliasing as a composition mechanism and most of the use of code scenes today are actually simple asynchronous speeding up simple simple asynchronous operations here's a buffer I read from a socket and I writes for a socket and if this one waits til there's something read this one waits til there's something to write and so basically the idea again and again is that these things have to work in combination and all of these principles on my list the rules of thumbs my list works in combination and one of the things we do is to build library from it a user shouldn't have to care with a feature is implemented that's how it is implemented as building or as a as a library facility and that that bill I actually used to say the library design is language design and language design is library design it's fundamental the same thing you're trying to allow the users to express their ideas directly and affordably and so we need great libraries the standard library should be designed with the same principles as the language facilities because well it is fundamentally the same thing I don't want to know whether a complex number is a building or a user defined type we have lots of libraries a lot of them are under standard and we should get more on the standard in the future and lots and lots of other languages I mean there there's at least an order of magnitude probably two orders who make and shoot between the nice stuff we get in the standard library and the nice stuff we get by downloading so the rules of talk what am I going to do here oh one of my favorite new libraries here is chrono and it gives us time points durations days months years and time zones now one of the things is that once you get today it's a calendar kind of stuff and time zones things get very messy this is not math anymore it's not computer science anymore it's a combination of physics physics and local conventions especially the local conventions are very tricky like the time zones or the the leap seconds things like that and the standard library chronal takes care of that so here is something I found in my mail one day from Howard Hinnant who designed kronole and so I looked at it a bit so he is using kernel and he says for all days starting from that date until the year becomes 2020 move forward two weeks so he's looping or two weeks period of a year and then he gives me that he hits he grabs a time in London plus 18 hours oh yeah it's six o'clock in London and then he writes it out he then gets the version of that time for for the standard Eastern Time in the US and just to show off I think he gives the Greenwich Mean Time the UTC now that is the share tool for the C++ standards committees Direction group so I printed it out and this is our meeting times you can use this for very useful interesting things I wouldn't have guessed that and I think most of you wouldn't have guessed this was a obvious and easy implication notice the for loop and the time zones and things like that that that one digs tunnels so except for implementation details and besides I did not mention all of these things sizes wrong pointers allocations dear occasions loop control costs macros resource or flows all this kind of good stuff that people talk about you can talk about C++ without going there don't go to that level unless you have to and we can do most of the things that we are using as an implementation at that level we have to separate what we want to do and what we can do and I'm encouraging is the right Martin C++ and this is hard because a lot of us have sort of internalized this kind of stuff over the years and anything that's new is often assumed to be useless complicated and too expensive it isn't on average so let's try and do it and we have to distinguish what works what's maintainable what runs and what's affordable and basically you can usually at the higher level right type and resource safe C++ no leaks no memory corruption no garbage collector no limitation of Express ability no performance degradation you don't have to leave standard C++ and the tools are coming along there's some Visual Studio there's some in client ID I'm looking forward to this kind of guarantees to be actually guaranteed by a program you can by the way never get there if you are writing 88 vintage C++ you have to lift the level of abstraction to the point where the analyzers can understand what you're doing and I find that one of the good measures of good code and good performance is that I can understand it if I can't understand it there's a good chance that the optimizer can't understand it and there's a good chance that there's bugs in it so the best way of optimizing stuff is to use good libraries we use the standard facilities then throw away the clever stuff and see if it still runs fast enough and often does and sometimes speeds up and so one of the interesting thing about this talk and the thing there was the inspiration of this talk is that these rules of problems are old they are picked from the design and evolution of C++ which was from 94 when I was forced to sort of collect the rules I've been using for C++ and I think this is one of the things that has given stability over the years the the language features changed the feature set change the implementations change the optimizer has changed but some of the fundamental concerns for performance reliability use of resources do not change and that that's why this still works so I was talking about C++ 20 at least that was so what is this to do with C++ 20 what is C + + 20 it's basically the best approximation of C++ ideals so far I expect C++ 23 to be a better approximation I expect C++ 26 to be a better approximation of that we are not at the end of history and we get a lot of useful features in C++ 20 but a language is not just the set of features and the language is not a layer cake I encourage you when you talk to people when you try to explain what C++ is don't start with C and then say and then in C++ 98 we got and then in C++ lemon we got and so on you will just tire people out before you get to the end start with the simple and elegant and then peel off a layer of the onion when you have to it's so superfluous the general-purpose programming language for the definition in pronunciation and use of lightweight abstractions that's still true it's not just the grab bag of features there's a set of ideals there's a set of design principles and yes a community as John pointed out it's an evolutionary process and it is a process in WT 21 so let's just remember the Standards Committee they've been hard at work for 30 years and starting out that was a group there and this was when we voted out modules Richard Smith and Gabby dos Reis were the main signers and that will help from lots of others and you can see the committee was very very happy when we got modules so this is old story about a journalist coming up to an accomplished person Allah says o K you have cured cancer and ensure word of peace so what are you going to do next people are never happy right you want more so C++ is is the best proximation it's not perfect of course but it's actually great C++ 20 I think should complete super +20 there are things you cannot know till you ship there are things you cannot put into the latest release because as a feature freeze we need a couple of years to actually learn what this is at scale so there will be a lot of completing things like getting ranges used more systematically that are a table and then we would like to see standard modules I would like my import STD instead of having to do it myself library support for cold chains is incomplete and executors which is the model of concurrency and finally getting networking in place is a good idea and if we have time static reflection more compile time evaluation but here with generation at compile time and functional style pattern matching there's been experiments with that there are people working on all of these things this is an approximation of what c++ 23 might be as suggested by Villopoto lining and actually supported by the directions group and well it's hard to make predictions but this is what I think we might be seeing in the future and so basically the executive summary is that C++ 20 is great the votes have started national body comments are being sent in we have a committee draft food it's out for that there will be resolutions or problems found in the next two meetings and hopefully in Prague in February we will vote in C++ 20 we we know the outline of it we don't know the last little details but this is not a detail talk it's not going to be perfect we're never going to reach there as long as we have haven't stopped evolving and you can go for the in-depth talk for all the features here and all the major parts will ship in sheep are +20 meaning next year and the reason I predict that is all the major parts of Sigma plus 20 is shipping somewhere today it's just that we don't have all the implementation shipping all of the major features today that is next year presumably before the is own Geneva managed to approve the standard we were supposedly getting this stuff earlier thank you [Applause] um how is kool-aid going to interact with the executor so will it walk a thread or censor extinct uses have still not finished I don't not know how cool routines interact with them but I believe that you get a choice and in particular as possible to stack up a stack of coal genes have been optimized at once to do lazy evaluation that was true a few months ago good morning thank you for taking my question keep Mikolas it's my name I work in a financial community one of the problems I faced is legacy code often on projects were working with code that's at least 10 if not 20 years old and as I see you present these great new ideas and concepts can you give me any advice as to how to migrate legacy systems often there's own a lot of resistance to changing these systems because they work and they've been hardened thank you I have a slight problem with the microphone the echo up here so if I get your questions likely wrong please either correct me or forgive me legacy code is a major problem we have well 40 years worth of legacy code and in many industries that means that there are code that are 20 years old that I sent off for the one organization they don't know how it was written the documentation isn't there the people wrote it weren't there and the unit testing had barely been invented and the regression testing is incomplete and that is a major major problem I think my approach is is twofold one is for new code try to use the modern features and write modern code secondly try to get rid of the old code either by encapsulating it or by actually really writing some of the older systems bottom-up we are not going to get anything near perfection till we get more to support for analyzing both old and modern code to detect problems so you need things like leak finders and I prefer if I can so get statically finders that is if you can write your code so that you don't have any leaks that that is the best way to do it we're not there yet there are research and deployed solutions for rule checkers leak checkers things like that but legacy problems is legacy code problems is common to all languages that has been successful legacy code often is just old code and if it still works it's really hard to get the bean counters to to understand that we are actually building up technical tests and need to clean up the messes what we have to eventually all the languages doesn't have as much legacy code as C++ because they don't have as much code but they'll get there if they succeed I have a question what was the one single feature in that appear to us it was plus eleven that had the most profound impact on the language and what in your opinion one single feature in 2020 supplies blast we'll have that similar effect I think however I'm going to cheat I'm going to give two of each the most important single feature that affects the way you you write code in C++ 11 is constable functions moving a lot of the complex template metaprogramming into simple ordinary programming with functions to produce values so that template metaprogramming can be reserved to the things that that computes types and things that doesn't fit into the functional pattern very well and this has been spreading like wildfire to the point where I am worrying that we are you doing too much of it the invisible part of C++ 11 that was most important is simply the support for basic concurrency we had been doing concurrent programming for ages before but has not been standard people have been using POSIX directly using macros and void star stars lifting the level of expression of the thread and locks kind of code to a type safe level and having a machine model underlying it that makes it make sense was the most important so we have the visible thing which is the context of a function and the invisible things which is basically the concurrency model similar for C++ 20 I think the thing that'll have the most impact on the way we write code is concepts because it'll make generic code much easier much more elegant and much more widely useful increasing the express ability of what we are doing and again taking some parts of what is now complicated simpler metaprogramming and trending into something much simpler template metaprogramming was a widely successful c++ 98 feature but it didn't have enough support and the fact that it was so heavily used over the years even though it was so ugly and difficult this approved its utility so we make we have to make it easier to do the things that was done with template metaprogramming the invisible part actually is the modules the module should with a bit of loc improve our compile speeds by say 5 to 10 times and that is a major thing it'll change the way we work what a fundamental structure of our programs will actually not change that's why I call it invisible the way you get that advantage major compile-time advantage is by cleaning up your coat removing disgusting dependencies saving the compiler for compiling the same thing 10 times over because you might have a macro that is different so yeah cheating I can't count to one got two but I think I have a point yes thank you hey so I wanted to go back to your don't pay you don't use principal which I very much strongly agree with but I had a colleague recently challenged me on that saying that if we had time zones 220 or we add graphics or whatever 23 and we don't use them as a company we still pay for them because we pay the implementers to implement those things in money not in cycles but I had a response for him that I wasn't quite satisfied with myself I'd be interested to hear your response to that yeah [Music] yeah somebody has to pay and I I think maybe my statement of the cereal head principle has been taken out of the context of writing code and looking at the generated machine code and such and into a different domain and I have for a long time I think its DNA had the philosophy that if you can do things once or a hundred times or a thousand times it's much much better to do it in then doing it in a million times and therefore rather put a burden on implementers then on users and I think that's what we're doing here a lot of users benefit I know huge industries that they hint critically on kronole with time zones and the works and they pay a price every time they use a time zone library and sometimes that prices and the dollars to library vendors and such all of that code goes away maybe for ten fifty thousand organizations on earth and your implementers get stuck with it how many are there five ten I think I just made a major saving for the community that that's the principle that that I can bring forward here but but I think the the principle was yanked out of context hello BRE thank you very much for the talk my name is karellen network Phoenix and here's my question two of the rules of thumb you mentioned were compiled compile time detection and systematic general resource management specifically the ownership management but there is nothing in C++ that enforces ownership control at compile time like the burrow checker at the rust programming language other any plans on introducing a new mechanism that will check ownership at compile time thank you I don't have any plans for a feature and forces over ship at compile time the the problem with that is that then all the old code that doesn't use it will break instead I'm aiming at this distinction between the code that we consider good and the control we consider legal and we use coding guidelines and statically enforced coding guidelines where we can enter the extent we can as opposed to changing the language to try to enforce something that we probably could I do not know how much C++ code is out there but I have been told off recently for saying hundreds of billions of lines somebody wants me to mention trillion and I'm not sure I can but we're getting there and that means the change in language to outlaw most of that is not going to fly so instead I want to aim for four code checkers and style guides because if you have a style guide that says you don't have any leaks and the sleek finders now static leak finders that proves that you actually can do it in the language if you want it but I don't see us deploying such a language feature thanks for the answer I think for the talk my name is Cyril I'm from Bloomberg going forward on the designing new project starting from scratch you discourage reliance on abstract base classes as interfaces as abstractions and possibly going forward is other any plans to phase out then a morphism altogether first of all it is very rare that we start really from scratch it is much more common that there is a code base already or that we start by including five or ten fairly massive libraries that were designed 10 15 20 years ago furthermore I'm still of the opinion that there are domains where a physical object oriented programming is a good and possibly even the best solution so I would not discourage the use of class hierarchies and virtual functions what I would do and do is to recommend people look carefully at the problem to be solved and see if it really is hierarchical and if it really does need runtime resolution and it really needs open sets of alternatives and if the answer to those things are yes use traditional hierarchy based object-oriented programming and my suspicion is that there is very significant sections of the fundamental rockin areas for which this makes sense and there's a much larger series where there are libraries and support systems that are structured that way mostly in graphics and GUI but also in control of hardware gadgets where you have an abstract class as stable interface to a series of gadgets say different models electro motors and such in other words I don't think everything should be attempted I think we have run out so thank you [Applause]
Info
Channel: CppCon
Views: 226,266
Rating: 4.7686276 out of 5
Keywords: Bjarne Stroustrup, CppCon 2019, Computer Science (Field), + C (Programming Language), Bash Films, conference video recording services, conference recording services, nationwide conference recording services, conference videography services, conference video recording, conference filming services, conference services, conference recording, event videographers, capture presentation slides, record presentation slides, event video recording, video services
Id: u_ij0YNkFUs
Channel Id: undefined
Length: 91min 26sec (5486 seconds)
Published: Tue Sep 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.