Exploring Programming Paradigms and Resource Management

Bjarne Stroustrup - The Essence of C++

Estimated read time: 1:20

    AI is evolving every day. Don't fall behind.

    Join 50,000+ readers learning how to use AI in just 5 minutes daily.

    Completely free, unsubscribe at any time.

    Summary

    In this enlightening lecture, Bjarne Stroustrup delves into the core principles and evolution of C++ with a focus on its application in real-world scenarios. Stroustrup explores the balance between low-level hardware control and high-level abstraction, emphasizing the effectiveness of C++ for systems programming and complex application development. The talk also covers advanced concepts such as resource management with constructors and destructors, generic programming using templates, and the role of object-oriented and functional programming paradigms in C++. Touching upon the challenges with modern coding practices, Stroustrup offers insights on improving performance and teachability, advocating for modernized code bases in high-performance applications.

      Highlights

      • Bjarne Stroustrup highlights the essence of C++ as an expert-friendly language that caters to both novices and experts. 🎓
      • The importance of teaching resource management using C++'s constructors and destructors for error handling and memory management. 🛠️
      • Stroustrup discusses the overuse of inheritance and suggests better practices for using C++'s object-oriented features. 🤔
      • C++'s ability to efficiently manage resources and improve performance, specifically through template and generic programming innovations. ⚙️
      • A discussion on how C++ can coexist with other programming paradigms and why versatility in coding is a strength. 🌐

      Key Takeaways

      • C++ offers a unique balance between low-level hardware access and high-level abstraction, making it ideal for systems programming. 🖥️
      • Resource management in C++ is efficient through constructors and destructors, minimizing memory leaks. 💾
      • Understanding multiple programming paradigms is crucial; don't be bound to just one! 🧠
      • Templates and generic programming expand C++'s flexibility, allowing for zero-overhead abstraction. 🧩
      • Old code often carries 'barnacles.' Embrace modern practices for cleaner, faster, and safer code. 🚀

      Overview

      Bjarne Stroustrup, the creator of C++, shares insights into the design and continued evolution of the language. He discusses the necessity of balancing hardware control with the ability to craft high-level abstractions, something C++ excels in due to its dual inheritance from languages like C and Simula. This makes it a potent tool not only for systems programming but also for tackling complex software challenges.

        The talk emphasizes the importance of resource management in C++, highlighting the role of constructors and destructors in maintaining program integrity. Stroustrup delves into the concepts of ownership and memory handling, laying out the rules for effective error handling and illustrating how C++'s model prevents leaks and other common programming pitfalls.

          Stroustrup also tackles the challenges facing C++ programmers today, advocating for adopting modern practices and language features that enhance code readability, performance, and safety. He posits that understanding and integrating various programming paradigms is essential, urging developers to move away from outdated practices and embrace the powerful capabilities of modern C++.

            Bjarne Stroustrup - The Essence of C++ Transcription

            • 00:00 - 00:30 and I just like to say that we and Morgan Stanley have been overwhelmed by the amount of support and interest in this evening's event um I'd like to firstly give a a huge thanks to bjn for giving up his time over the course of the next two days um he's going to be presenting his lecture to probably close to 800 people between Glasgow and
            • 00:30 - 01:00 Edinburgh which must make it one of the biggest Tech events that I can remember recently in in Scotland um it's fascinating to see the spread of people in the audience we have everything from people who have managed to find out about the event who are still at school um through Academia students and I think there must be about um 50 different Scottish technology companies with representation here so a bit of
            • 01:00 - 01:30 housekeeping for me first off can everybody make sure that their mobile devices are on silence there aren't any fire drills planned for this evening so if the fire alarm goes off the exits are at each side at the front of the auditorium once we go across the road we we'd love you all to come across and join us for some drinks nibbles and a bit of networking um across in the informatics Forum right across the other side of the square and again the fire doors there's one on each wall there and
            • 01:30 - 02:00 but for any disabled guests either go out the back wall or out main front entrance in terms of people I'd like to thank firstly my own team I'd like to say a massive thanks to Rebecca Hastings and Joe froley who have managed to pull together these events in just a couple of weeks I'd also like to thank Khaled Rafi and his team from Morgan Stanley in Scotland um for all their support in making this this happen and thank professor Dave Robertson and the
            • 02:00 - 02:30 Edinburgh University School of informatics as well for their support after the event when we go across for the drinks and canopies you'll see that there is a popup there from bite night which I'd like to take one minute to promote it's the it industry's annual charity event to raise money for Action for Children it's a sleepout that we do in October in Hollywood Park we're aiming to get at least 200 um techn ologist to sleep out
            • 02:30 - 03:00 this year and last year we raised over £100,000 in one night and it's to help prevent the causes of Youth homelessness so there's 500 people here tonight if I can try and persuade even a fraction of you to go and speak to them and find out a bit more that would be fantastic so without further Ado I'd like to welcome um Phil Carter who's one of the managing directors for technology at Morgan Stanley
            • 03:00 - 03:30 hi there um just checking you can hear me do I need to use the mic no no that's good um so my name is Phil Carter I'm um I work in uh Morgan Stanley um so I run the uh Equity trading systems um uh here in Europe and focus a lot on derivatives technology and so on and so forth I hope hope that you watched every single slide
            • 03:30 - 04:00 out of the 160 slides that went up there um whilst you were waiting for the lecture to start um so I won't have to tell you much about what we do but um anyway the I don't understand between you and Beyond getting started I'm looking forward to his lecture too um I started my career very much um working as a c programmer trying to convince my managers at the time were a big Fortran Pro programmers not only to move to see but to move to C++ um and that was about
            • 04:00 - 04:30 20 years ago and I I I succeeded in doing that I'm I'm quite pleased and that was one of my biggest um uh achievements in life I think um anyway the to tell you a little bit about um Morgan Stanley who um you know we have the great privilege of having Bean to join us and I thought I'd just tell you a little bit about us the um just briefly I think you know before I got into investment banking technology I
            • 04:30 - 05:00 worked in the defense industry and I didn't have a clue what those kind of bean counters did so um but to now I've been in that industry for uh 15 to 20 years now the um you know the way I think of it more and more over that two decades is is very very simple um you know if you think I think of the firms like Google which is essentially in my view an advertising company where the technology is really fundamental to that business um you could say that for many
            • 05:00 - 05:30 many other firms LinkedIn really a recruiting agency with technology being fundamental well more and more I see what we do as being the exactly the same thing and I think you know maybe the cynics would say well of course You' say that um but really you know I I can say that with my hand on my heart because um we really couldn't run our business without it we don't run technology to support our business we run it because it's fundamental to our business um and you know one of the things we're very
            • 05:30 - 06:00 proud of is that um in for example the area that I work in we're number one in equity trading in the world and here in Europe and that is um you know a big part of that is because we like to think we build um far superior technology than our competitors so we spend a lot of time um trying to attract the best talents that we contract around the world um we also try and do the best we can to nurture the talent that we have
            • 06:00 - 06:30 um and they include languages such as cc++ where we invest heavily in that but also in other languages um and ban will kill me afterwards but you know other languages like Java more recently Scola um and uh many many other things so um you know we've recently uh built a new team building some of our most sophisticated Platforms in in Glasgow so we just uh I was just told we we've now hit the 100 Mark you know so we have 100
            • 06:30 - 07:00 programmers up in um up in Glasgow working on these systems alongside three or four other key Development Centers we we we have um in the world including um uh Budapest here in Europe and in Asia we have Shanghai and and Montreal in the Americas so you know we we really look for and I personally and I've been working um with with teams here in in in Scotland and in Glasgow in particular for the last 15 years and I've never had any problem with track great talent so
            • 07:00 - 07:30 with that I'm you know very pleased to come here with uh a great like bean and um you know be part of a lecture like this so I'm looking forward to sitting down there and uh and and listening to the lecture so um thank you very much and hope you enjoy the lectures I'm sure I will [Applause] right so um welcome um and and first of
            • 07:30 - 08:00 all I I don't go around killing people um and as a matter of fact I don't think you can be a professional in the Computing industry knowing just one language so if if you actually want to be in the field uh you should you should learn at least two preferably five uh maybe a few more uh but to today I I will of course talk about C C++ and I'm
            • 08:00 - 08:30 I'm going to try and and see what's sort of special what's essential to to C++ and I'm not trying to to drill down and and write and give you a really Advanced um deep technical talk I'm trying to get some of the key ideas across with a simple presentation as I can get it and I'm going to use examples for a long time uh scale because I think
            • 08:30 - 09:00 it is important that systems live for a long time uh software tend to live uh quite a lot longer than hardware and um people have this notion that you get a language right the designer figures it out and it's all there tomorrow it's not takes about 10 years from inception to uh serious industrial use and then it takes more decades for uh everything has get done
            • 09:00 - 09:30 so that's what I'm going to do there are some pictures here they all have something to do with C++ except for a few that obviously has nothing to do with anything they're just Landscapes and such they are for me to calm down take a deep breath and remember I'm going to shift the topic but uh the ones that that appears to illustrate something probably has something to do with C++ that happens to be the tunnel of the large hon collider at CERN
            • 09:30 - 10:00 and um one of the things I really like about programming languages about it is it is a perfect excuse for sticking your nose into interesting projects in just about any field and so if you're interested in high energy physics and the structure of the universe uh being a programmer is one of the best ways of getting there it's uh probably easier than becoming at the radical physicist uh okay so I'm going to first talk a little bit about uh what C++ is
            • 10:00 - 10:30 and what it's supposed to be and um how how how we are getting there and then I'm going to go through some practical details I'm very interested in Resource Management mostly because I'm into systems programming and building complex systems where resources has to be dealt with dealing with objectoriented programming class hierarchies which I assume most of you have seen then I'm going to get into a bit of uh template programming generic
            • 10:30 - 11:00 programming with templates and then I'm going to point out that you know this this is a real world language with real world problems we can do better still um so um I I listen in to uh discussions in in in in real life and on the the web and it seems that often I'm the only one around who doesn't have a firm opinion about what's C++
            • 11:00 - 11:30 is um here here we see the uh Blind Men and the elephant and if all you've seen is sort of the tail you have some some firm idea of what the elephant is like except it's only right for the tail so everything we see here is is probably true for somebody at some time in some place and if you think that's the whole story you're wrong and so my problem is I actually know the Beast and it's a
            • 11:30 - 12:00 little bit harder to generalize what what the Beast is when you have to take it all into account so I'm trying to to to explain a little bit about the Beast here about that elephant so let's go back and see what I I I would like what I'd like when I started C++ what I uh have learned over the years that I want uh type safety is is a really important aspect and um when I started out out uh
            • 12:00 - 12:30 a c program couldn't check the types of its its function arguments there was no function prototypes there was no function declarations that specified the types and this was actually fairly controversial at the time people say well you know if if if you have a declaration of type then when I look at my call I do not know what it's doing because it depends on something I can't see I much rather have it just like an ass simply code I just do something and I don't have to look anywhere else okay
            • 12:30 - 13:00 so I have actually been trying to increase the type safety of the cc+ plus family of languages since uh day one and uh that's not all that easy because it's under constraints of size of programs performance of programs and such uh resource safety I I really don't want to leak things if you are writing um code for for say a spaceship uh you you should not run out
            • 13:00 - 13:30 of resources the resources can be just about anything but I think about things like memory file handles communication connections uh locks uh things like that performance my my my business by and large self-chosen is to be able to among other things uh run as fast as possible uh close to the hardware some language has to be able to implement a memory
            • 13:30 - 14:00 manager and a virtual machine and things like that and C++ more or less by definition is that that's the aim predictability if you get into a hard real time uh you really have to have every operation be executed in a predictable time usually a low constant maximum time and operations that can't do that are just simply not
            • 14:00 - 14:30 allowed on planes after um they take off I'm not talking about the educa entertainment system of course um I'm talking about the engine controls and the things that deals with the flipping of the wings um and C++ follows that rule roughly except for the use of free store dynamic memory which is not allowed there anyway and you have to do something else uh teachability it really should be possible to teach how to do this kind of stuff how to how to use
            • 14:30 - 15:00 something you can't just um assume people will want to learn something you have to make it reasonably easy and the complexity of the code should be proportional to complexity of the task you're doing that is a very big statement very hard to do it's an ideal it's something you can try and approach and in different domain you can get closer than others but it's definitely what I would like to see and I would like to have the code be readable by humans and machines again those of you
            • 15:00 - 15:30 who know C++ knows that this is just an ideal because you can write really ugly code in C++ as a matter of fact you can write really ugly code in in any language but this is C++ I'm concerned about it should be possible to write really nice readable code and you can um and I want it for systems programming embedded systems resource constraint systems and for large systems uh that's the sort of the domain I'm most interested in um it
            • 15:30 - 16:00 is not optimized for for writing simple web apps for instance it's not it's not meant to be the simplest programming language on Earth it's supposed to be the simplest we can get for real world constrainted applications uh C++ is is is expert friendly it's uh very expert friendly I uh Co the phrase to point out that that's not enough if it's only expert friendly and it's hostile to new
            • 16:00 - 16:30 users to novices to Casual users uh we're not helping a lot of people that could be helped so it has to be knowledge friendly too uh that that's an important thing uh and I'll show you some code that makes this statement plausible um C++ comes from uh two traditions and programming languages and quite consciously so here is this line here here which is deliberately uh
            • 16:30 - 17:00 towards the low end this is sort of close to Optimal exploitation of the hardware very abil the ability to manipulate anything you have in hardware and um and do it well so we started with a simpler we got Languages by bcpl and C uh C is still around um this is dennise Richie and his genius was actually to define a model of Hardware that was sort
            • 17:00 - 17:30 of general and could be used I mean if you add something use the add instruction if you access memory memory are sequences of objects when you want to compose something out of uh other objects you just put them next to each other you stack things of the same type next to each other and you get arrays you stack things of different types on other and you get struct uh that's a very nice memory model it has stood the test of time very well however this is lowlevel stuff um I don't really like
            • 17:30 - 18:00 programming with bits and bites and addresses and things like that we we we need something that uh is is more fit for humans and and here we have backers who invented forine forine was a language designed for a very particular kind of humans engineers and scientist and um it was the first language to lift the uh level of programming from being
            • 18:00 - 18:30 focused on the machine to being focused on the problem it was the first language that really was fit for humans and once that was done um we actually got portability I mean for Trine was designed so that IBM could sell more machines and it it turned out that after for Trine you could take these programs and with fairly minor changes mve them to uh say a UNC which was a main
            • 18:30 - 19:00 competitor at the time and that was I believe an unintentional effect but it was very good for us and lifting things up to a human level is is a good idea however as I pointed out scientists and Engineers are very peculiar subspecies and um so every once they had their own language everybody else wanted one too cobal being the most Pro um uh prominent for for business but basically very soon we had um about couple of
            • 19:00 - 19:30 hundred languages the problem was they couldn't talk to each other I mean when you write something in cobal you can do really nice record and file manipulation that was what it was for that was what the business people wanted it could add and subtract but anything more sophisticated than that it was not very good at for Tran could was really good at linear algebra for computations could handle floating Point numbers now what do I do if I as a scientist wants to read some
            • 19:30 - 20:00 records manipulate them and compute on the result the answer roughly was you're out of luck and um there was many many solutions to this but the Breakthrough I think was this this is CH nug with the language simul and said why why don't you let people build their own abstractions instead of building in um matrices and records and things like that why don't you just say give people
            • 20:00 - 20:30 the language facilities for building their own uh abstractions into the language so if you want a vector you build a vector if you want a 3D Matrix you build a 3D Matrix and if you have a record you build a record that's where the class uh class uh concept came in and uh they used uh hierarchies of classes to represent related Concepts some concepts are hierarchically oriented not all and uh basically you got what's currently
            • 20:30 - 21:00 known as object-oriented programming languages with classes and class hierarchies and um I I found myself in a project a long time ago at Bell Labs where I needed to manipulate the lower levels of the hardware WR memory managers write uh uh network drivers but I also needed to construct something that had a fairly High complexity of organization I wanted to be able to say this bit here runs there this bit there
            • 21:00 - 21:30 runs there no I changed my mind this bit here should move over there it was a distributed system languages at the time that could do the heart uh close to the hardware programming could not say what a bit was was sort of a ball of code it was there was no no serious abstraction there and so I took the simular class concept and bu build it into C and uh some of the terminology uh comes from simula and has later been inherited by
            • 21:30 - 22:00 lots of other languages and uh this happened in 1980 and we've been going out for a while since uh we have a new standard there in 11 we're going to get a new one uh this year um it's it's all the technical work is being done it's it's going through voting procedures under the iso so um if you want to explain what C++ is it's from this idea of taking the C and taking simula and uh
            • 22:00 - 22:30 putting them together so we want a direct map from to Hardware between the inst instructions and data types in C++ coming initially from C and we want zero oread abstraction the ability to take these things wrap them into uh new types with data representation and the um and and uh and set of operations or uh interfaces def defined and then being implemented later
            • 22:30 - 23:00 initially from simula uh one of the really key things that came in very very early in the C++ um development very early being like the third week or thereabouts Constructors and destructors because if you want to build your own abstractions and you want objects of them very often the um the object needs to be initialized and if it holds something that needs to be cleaned up you need to call a Destructor to do it and so a lot
            • 23:00 - 23:30 of it came in here as the advance uh in in understanding and the number of uh abst kind of abstractions we dealing with uh could could be done and basically much of the oper of the inspiration at the time uh came from operating systems my my background is in operating systems and microprogramming machine architecture and such that that explains a lot for good and bad um and basically I've been wondering
            • 23:30 - 24:00 this question what does C++ want to be what uh when when it grows up and the answer is wants to be better at that that is c++'s domain if you're in a different domain you will probably want different languages and one thing that people point out is that there's a lot of old cron C++ it's like a ship that's been at C for a long time it has Acquired barnicles and such and we would all like to get rid of them
            • 24:00 - 24:30 but we can't because long-term stability compatibility is a feature people say we want all of these new great things but we do not want you to break our code um this problem will probably be addressed in the future through code analysis and code transformation I hope we'll see more of that but basically uh remember this one uh if you want C++ to be different and better um the next question is can I break your
            • 24:30 - 25:00 code and I think I know the answer uh at least for people with large code bases that's that's important as opposed to a hobbyist and so here's my version of the elephant it's a language designed uh for building uh basically um efficient and elegant abstractions uh four areas like software infrastructure resource constrainted applications
            • 25:00 - 25:30 okay so this is one of these pictures that are there to get me to stop take a deep breath think now comes a new topic which is Resource Management how do we uh deal with with with resources um basically a resource is something that you have to acquire and then when you're finished with it you have to give it back to wherever you got it from and uh this can be explicit it can be implicit but at Le that's the definition of resource the example is
            • 25:30 - 26:00 manage is is is memory give me some memory I have to give it back otherwise we have a memory leak um it's a file handle give me open the file close the file get a lock acquire the lock release the lock open a socket connection close the socket connection things like that and uh we we we don't want leaks we we really don't want the the the the the the space probe to to run out of of anything uh we don't want our um say
            • 26:00 - 26:30 cell phones to to crash once a day because it runs out of anything but electricity we can't actually handle battery life like this except of course if we run more efficiently we'll use less battery but that's a different issue um and usually I mean a resource should have an owner because otherwise it's it's really hard to know who's responsible for um giving back resources uh the fact that um there's no obvious representation of an owner in a
            • 26:30 - 27:00 in a language like C or C style C++ is why these programs tend to leak um say I make myself a shape let show up on the screen and I get appointed to that shape who is responsible for uh getting the screen off getting the shape off the screen again is it the screen manager or is it me okay then I give the point pointed to the shape to him is it now his job or is
            • 27:00 - 27:30 it mine that is a source of a lot of problems in in SE style programming we need to deal with that so basically the rule is there should be an owner that's often a handle like this that points to uh the the real um resource the real object and uh basically the handle should represent something meaningful it could be a vector it could be a a a pointer to something could could be just
            • 27:30 - 28:00 about anything okay so uh in the C++ standard there's actually a lot of things that behaves like that you see all the sort of the main data structures vectors strings Maps hash tables and such all follow this model uh and so whenever you have one of these it's a local object and it points to something that it manages over on the side and uh when it goes away goes out of
            • 28:00 - 28:30 scope it cleans up its mess after it and it's not just uh memory threads logs files file streams and share pointers are in this category so this is all over the current C++ standard and basically they all sort of more or less look like this uh here is an abstraction which is a vector of T's and so you can initialize it in some way all the interesting classes have some way of
            • 28:30 - 29:00 initializing them in this place of case you can give it an initializer list of Tes and initialize a list of cheese is sort of a list of things and curly braces so uh here I'm saying that if you want to make a vector you have to give me an initializer list it will then make a a list and it will actually represent it somewhere it'll set aside some memory it'll put the elements of the list into that um Vector into into that memory it keeps track of how many things they how
            • 29:00 - 29:30 many things it's got how many elements it's got that's about the simplest you can do and now you can make a vector of doubles and initialize it a vector of strings and initialize it and the important thing here is that when they go out of scope the whole thing gets cleared up notice this is not not the simplest of memory managements at least Vector of strings strings are themselves handled so to clean up that Vector of strings I have to tell the vector to clean itself up and the vector has to
            • 29:30 - 30:00 tell all its strings to clean themselves up and we could do more complicated things like that having a vector of records with locks uh associated with them things like that but anyway it's very simple model you can create things and you clean up your mess when you're finished the clean up is implicit and so here is a a sort of a classical example uh you find it all over million line code bases and it's a Miss
            • 30:00 - 30:30 let's see what's wrong here I want a gadget and um say say I'm a Java programmer or a C++ programmer that still thinks it's cool to write code like in the in the '90s I make myself a gadget and I assign that that to a pointer the pointer I get back and um there's a lot of code like that the point here is I didn't I won't tell you what's in the gadget but that could be all kinds of interesting things think it
            • 30:30 - 31:00 has a file handle for instance uh it opens a file or something so here I do some code that might throw an exception I do something that might return and then I delete things and all good Java programmers will complain that um the there has to write to delete that's such a pain in the neck and we know that if you actually have to write such code you get it wrong a fair amount of time uh as a matter of fact my rule is days is if I see a new I know I have
            • 31:00 - 31:30 to write at leite somewhere therefore I'll get it wrong therefore I shouldn't write the new but let's see uh a garbage collector could be an idea you just take away this one and everything will work except that we don't know what's inside the gadget it may hold things that uh needs to be cleaned up so we won't do that so um then the naked pointer is a problem also if I throw thow an exception I have leaked the gadget if I return I have
            • 31:30 - 32:00 leaked the gadget this is actually not good code so we can do something like this uh this is fairly mod than C++ it's been been used heavily for the last sort of 15 20 years in places we have a shared pointer I'm here using a C++ 13 uh notation for it says make me a gadget and return a shared pointer to me a shared pointer is sort of a simple implementation of garbage collection uh the first garbage collectors were were
            • 32:00 - 32:30 counterpointer things and several languages call it garbage collection to this day the point here being that if I throw an exception the destructor for that Shar pointer there uh fires and cleans up the mess similarly if I return the mess is cleaned up my code gets simpler there's no delete down there the whenever we get out it gets cleaned up fine Constructors and destructors handle
            • 32:30 - 33:00 this problem there's only one problem left is I'm not sharing anything that Gadget is mine all mine so why do I need to do things like a a use count to keep to to count the number of users of um of the gadget I know the number of users it's me one so we can simplify that if you want we can make a unique pointer to a gadget and everything works simple it's simple
            • 33:00 - 33:30 and cheap one of the problems with uh shared pointers is they have to maintain a use count a use count is a shared resource it's shared between all users of the pointer and so when we make more pointers to that uh shared object the use count goes up one two 3 two when it gets to zero the last guy out cleans up the stuff cleans up everything and we we don't have any leaks of file handles
            • 33:30 - 34:00 and such but it it still if there's nothing shared I shouldn't introduce a shared resource we are in a world where lots of forms of concurrency happens and anything shared cost more than you imagine it does so this is better but you know I own that Gadget why am I dealing with pointers I should have just done it simply in the first place there are no need for any pointers here I just make my Gadget and the destructor of
            • 34:00 - 34:30 Gadget gets thrown if we get out of here we can get out here we can get out here we can get out here doesn't matter Destructor is called clean up or the exceptions and that is good and basically we've reached the point where the code looks simpler and there there are no pointers there are some behind the scene but it's easy to write a in a Constructor and a Destructor pair that takes care of resources so this is
            • 34:30 - 35:00 good and um this this this has known by the amazing phrase resource acquisition is initialization which I consider the proof that I shouldn't be in marketing and uh basically it just does what we said and it happens to be a good way of dealing with a system that uses exceptions and exceptions is the best way of dealing with error handling because it will give the right thing right have things had to happen
            • 35:00 - 35:30 especially when you have destructors to help you clean out local resources and so we can write leak free code and in general what you do is you establish an invariant in the Constructor and tear it down if necessary in the destructor fine and if you don't need a Destructor well don't write one now um where do we then use pointers we use them as references and iterators so we don't use them to represent
            • 35:30 - 36:00 ownership we use them to refer to objects that are owned by somebody every object should have a uh an owner and this leaves one problem this problem has been there for a long time in the C++ Community people tend to want to return a lot of stuff from a function to its caller like make something like make a shape make a matrix of 100,000 elements
            • 36:00 - 36:30 something like that you tend to use new to put it over on the free store and then return a pointer and the minute you do that all the problems with pointers come back they you have to decide who is in charge of the object is it the creator of the pointer or is it user of the pointer and if I share the pointer how do I know who I shared it with you basically can't solve those problems so let's let's deal with this as we've always been able to deal with it a lot
            • 36:30 - 37:00 of these things are not new a lot of these things are not original but we have easier ways of doing them now and that's important it makes it easier for sort of the the the the the mythical average programmer uh to deal with it so let's consider where this picture happens the idea that you make a big thing and you need to pass it out of a function my examples with the gadget and such all worked within a single scope
            • 37:00 - 37:30 now we're talking about communicating between Scopes so there's limits to what I can do with Scopes and this happens with Factory functions functions that make things and functions returning lots of objects containers and things like that so let's take a classical example take an operator Plus on matrices a matrix could be big right so I take in two matrices and I'm supposed to return a mat Matrix that's the sum of the others so make a new Matrix fill it up
            • 37:30 - 38:00 with the good stuff and return it back now the Matrix may be 100,000 by 100,000 doubles so even on a modern architecture you can tell that you probably shouldn't copy it on the way back so let's try and return a pointer that's actually not such a bright idea we get the pointer problems but it also looks ugly uh I I want to deal with matrices not pointers to matrices and I don't want to go around D referencing uh
            • 38:00 - 38:30 matrices to get the result and apart from that how many of you spotted the bug before I now pointed out to you there's a bug right there this should not compile in any decent program and it won't so anyway people say they like references better than uh pointers so let's try this we we pass in references after all by pass by reference that's good let's try and return by reference now this code uh looks nice uh except who deletes
            • 38:30 - 39:00 the object who who who who says delete to the pointer well which pointer where is it it's I've hidden the problem that doesn't mean that the problem went away this is almost certain to leak even faster than the examples I've shown before um then people have solved this by saying okay so an ad operation for um M matrices something that generates big object should take its arguments by reference and return its
            • 39:00 - 39:30 argument by reference now you have to set up a Target and then uh program against it now we are regressing to a simply code we just lost 300 years of experience of how to write down math and related things uh I I I really don't like that this this works in places a lot of this works just like dealing with the pointers directly it works if you have enough time to deal with it your programs are simple enough but often
            • 39:30 - 40:00 they aren't so we have to deal with it and the obvious thing is that if you are making a new object you should return a new object right the plus operation says make me a vector where the elements are the sums of the elements of my arguments so I have to return a um um a matrix the problem is that the semantics of returning is the semantics of
            • 40:00 - 40:30 copying sometimes uh compilers can figure out how to optimize that but it's not guaranteed uh so what we do here is that we return a 10 million uh elements and it's painful so we try and do tricks and these tricks by and large are non General and and tricky brittle that's not good so what we should do is something else and and the the key here
            • 40:30 - 41:00 is something that we all learned when we were about 6 months old right here's an object here now it's all here there's nothing there it's so simple as that now only a computer scientist could get the idea that to get this one over there we make a copy of it put it over there and then destroy the original it's just doesn't make sense right but that's what people have more or less been doing for a long time or
            • 41:00 - 41:30 they buil some handle to this one and moves the handle L they leaving the original in place or some other complicated stuff we need to have something really simple and the really simple looks like this okay very naive I want a matrix that is a sum of the that has elements that are the sum of the two things and then I return it that code should work and we can make it work because we can make we can simply steal the representation I make R it's there
            • 41:30 - 42:00 it's filled with all kinds of good stuff I want it over there so what I do is I take this poter put it over there and make this one points to nothing so that when the destructor comes and cleans up the mess there is basically no mess to clean up and as I mentioned there have been compilers over the last 20 years that could do this kind of trickery by themselves because it was an optimized version but here I'm not relying on any optimization so we are moving the
            • 42:00 - 42:30 matrix by doing two word copies to copy the pointer and if the optimizer works we can get rid of that copy too because well that can be optimized away um anyway so this is dirt cheap and uh the way that code is done is we write what's called a move Constructor we have Constructors that construct things from basically arbitrary things we have copy Constructors that Define what copy means
            • 42:30 - 43:00 we have move destructors that defines what move means uh this notation here and the best C style is cryptic and uh uh that means a move Constructor and so basically copy the representation set the representation to nothing this is it now this code works and it's efficient even if there's 10 million elements here so that is actually very simple and by the way you don't have to write all of these
            • 43:00 - 43:30 Constructors yourself if you if your representation knows how to copy and move so do you the language will uh generate the constructors and destructors for you so if I defined a matrix as a vector of doubles that's the elements and by the way the vector of doubles of course knows how many doubles it contains then I don't have to write any more code because if you move a matrix you move its representation which
            • 43:30 - 44:00 is the element which knows how to move and you're finished this code here is efficient uh and leak proof exception proof and uh dramatically simpler than some of the code we have seen over the last many years okay that's it so here's my comment about garbage collection uh the garbage collection is neither General nor nor ideal it is not General
            • 44:00 - 44:30 because uh it only takes care of memory and a lot of my resources are not memory and it's not ideal because it imposes a a shared resource on the system so some of the old uh Sun servers had you could get them with 64 processors and a lot of the time 63 of the processors sits waiting for the 6 4th to garbage collect we're better today somewhat better but
            • 44:30 - 45:00 it's still fundamentally a shared resource in a distributed world where uh we we we don't actually need to impose sharing so I you may have heard of the notion of false sharing um there's an equivalent for garbage collection so what do you do keep your data in containers like that Matrix or that vector or that map or that hash table or something like that and manage it with with ra AI uh the Constructor Destructor
            • 45:00 - 45:30 uh stuff I showed you use move Constructors so they are essentially free to move around copy Constructors when you want to copy use Smart pointers if you need pointer semantics when do you do that say take the old shape example if I want a pointer to the interface class shape and I can uh deal with pointers to circles and triangles and heaven knows what um you you just use Smart pointers if you are the only owner and you just want to pass it
            • 45:30 - 46:00 around you can use your unique pointer if you are using a shared pointer if you're sharing this the the object so that you don't know who's responsible for deleting it you use a sharp pointer I mean when would you need a share pointer a classical example is that you pass some data to three or a dozen tasks that runs in in parallel uh hopefully for you you either have
            • 46:00 - 46:30 some locking mechanism or you have pass a pointer to const um and now the last of these threads task concurrent tasks is supposed to clean out and and and and delete um the the object pointed to the shared object and the shared pointers work really nicely for that but if all you do is to see the use count go 1 2 1 Z that's not the optimal way and that's what the way they actually mostly used
            • 46:30 - 47:00 in existing code people use them to pass out information from inside functions uh now uh then if you're still leaking usually for for old code uh you can do what's called litter collection that is you ever so infrequently run the garbage collector like once every day because there's not that much garbage so you don't have to run it all the time and and there's a standard interface to that kind of of collectors and they
            • 47:00 - 47:30 actually work rather well if you have a a lot of old code that you can't convert to the the modern style so let me show some Modern C++ code that sort of illustrates this um here I want to find all elements in the container C that um that that has the value V this is just a trivial example of course so what I return I return a vector of pointers to the elements the value type of a
            • 47:30 - 48:00 container is the type of the elements and so the way I write this function obviously is make myself a container to hold the things that match and then for all X and C if I a match push back its address and I'm using a fair number of new C++ features here uh there's Auto that says find the type statically from the element uh from its initialize ER and so obviously this is the element type of the container uh I passed things
            • 48:00 - 48:30 by reference here I don't want to have copies um and then I return by value and now I can write things like this where I had a little lamb uh for all P all pointers and that comes back from find all test that I actually um got got the right thing back um it's a little test program that just test the sanity of this but the point being it it may not
            • 48:30 - 49:00 look like the C++ you're used to uh in C++ 98 there's a major performance bug right there but fortunately you can't write it in C++ 98 because I loaded it up with a few C++ 11 features if I'm going to use um move semantics I am definitely loading up the program with a few explicit features so that it breaks if you try and compile it with a C++ um 98
            • 49:00 - 49:30 compiler I'll much rather my code breaks than to get a performance bug uh that you might get there okay let's look at object oriented programming I'm going to be brief here because you all probably know it better than I do so here's the good old shape example here except that I take a vector of unique pointers to shapes so this is a classical example of where you want a polymorphic pointer I uh load the vector up with a couple of
            • 49:30 - 50:00 objects which I create using make unique says make me a circle and give me back a a unique pointer okay not note the absence of news note the absence of uh raw pointers that are just a representation of the address in memory and now I said draw them all I go for all of them and call draw I I'll will get back to what a drawable sequences later um so that that's fairly simple and it
            • 50:00 - 50:30 is the classical objectoriented example except that this one here is is fairly brief and it doesn't leak at all um of course a shape looks the way it has done for a long time borrowed the word virtual from the simil guys that invented the mechanism and I B just as I borrowed the word um class from them um why it isn't called type well it was simul was invented by mathematicians and
            • 50:30 - 51:00 it seemed natural to them um and then the only thing that's new here is that you can be explicit about overwriting you want to say there must be a draw with this type it takes no argument return to void in the base class for me to overwrite or get some checking it's just slight little uh refinement that some people like um you don't have to do it if you don't like it the prot protection model is public protected and private uh Beware of protected it will
            • 51:00 - 51:30 cause bugs for you if you use it for data there are people who think it is really cool because it allows anybody to molest my data um sorry uh there's usually a different explanation but that's what it is you opens the um the the representation to be modified later it makes it really hard to have an invariant so this one here is for functions only we we have multiple inheritance so does of course every language that provides a static
            • 51:30 - 52:00 interface it's just that they only provide interface inheritance we have implementation inheritance too we can discuss that if you want but interf we we need at least interface inheritance uh inheritance or abstract classes that class is abstract it has no uh full implementation there's minimum runtime type identification we have not gone into the uh sort of the serious uh reflection kind of world okay so
            • 52:00 - 52:30 object-oriented programming inheritance is great when you have hierarchical [Music] um Concepts in your domain that's why it is so nice with the shape example because obviously a uh smiley face is a circle that's a shape or something like that um you can get into trouble even then but basically it's uh if you have a domain where you have hierarchical
            • 52:30 - 53:00 relationships it it works fine the indirect function called the virtual functions are fairly cheap I have been told by measurements recently that this figure is too high uh that is it's closer to um to zero overhead than than before if you call it a few times the branch predictors have been improving uh dramatically so I am actually being pessimistic here but the main problem is it doesn't um doesn't inline well if the
            • 53:00 - 53:30 operation you want to pass along is is is something really simple you don't get inlining and in high performance uh uses that's important and anyway you tend to use allocation deallocation etc etc this is pretty good though you do get do get greater coupling between programs uh Parts you can mitigate that by having better interfaces abstract based classes but basically inheritance is seriously
            • 53:30 - 54:00 and systematically overused and misused there are people who think good programming means object-oriented programming and objectoriented programming means use of enormous hierarchies certainly the first part of that statement is wrong there is lots of uh good code that are not objectoriented and there's lots of lousy code that are objectoriented by that definition um okay so uh that said I go on to the next
            • 54:00 - 54:30 thing which is generic programming which has been widly popular and successful in the last sort of uh 10 15 years so let's look at that um I'd like to point out that the first paper I ever wrote on C classes c++'s and uh predecessor uh included generic programming and I explained why I really wanted a vector of T where T was a parameter and I really wanted operations
            • 54:30 - 55:00 of vector of T's that had t as a parameter I mean if I have a vector of T's I want to sort a vector of T's I don't want to to fix t uh arbitrarily for operation so I wanted that and I I wrote blindly that um you could do that with macros that did not scale it's lousy solution uh in 87 or so I decided that we needed something better we needed something extremely General and
            • 55:00 - 55:30 flexible I never trusted the science that could only do what I imagined and I want a zero overhead because I wanted to put C arrays out of business if I could the C array is my least favorite data structure it is the ideal structure for uh accessing memory directly but most of the time that's not what we want um CR arrays has a a tendency of losing their their uh size I mean with the slightest
            • 55:30 - 56:00 excuse an array turns into a pointer to one or more elements actually turns into a point yes that's what it does one or more elements except that other pointers can can refer to zero or more elements this this is the source of a lot of problems so basically I wanted to compete with that because I wanted data structures that for starter did not uh convert to pointers and remembered their
            • 56:00 - 56:30 size and then I wanted well specified interfaces I pointed out one of the very first things I did with with with C++ with C when I built C++ was right better HP checking so obviously I wanted interface checking for for this and I looked at it and at the time nobody knew how to do those three and I couldn't figure out how to do it given the constraints of My overall problem I had to pick the two first and you know two out of three ain't bad but it doesn't
            • 56:30 - 57:00 feel good it doesn't feel right and so I've been trying to solve that uh ever since and I actually do have a solution which I'm going to show you in in in not too great detail but let's uh first see how uh templates have worked up till now uh you can write yourself you can parameterize a type then you can parameterize an algorithm with a type and most of the time you have to be explicit about the uh type of a type
            • 57:00 - 57:30 sorry the the parameter to a type you don't have to be it for a um for a use of that type you simply deduce what T is if that V happen to be a vector of integers obviously the T that parameterize sort can be implicit and is it'll be a sort of integers if it's a vector of strings it will be a ve it will sort strings fine um what you can
            • 57:30 - 58:00 do here is anything that you uh you you can call it compile time dock typing dock typing is a general principle that if it walks like a dock and it quacks it's a dock in other words you just use the argument type and if it worked it worked and if it didn't work you get an error message the only good thing about the error messages you get is you get at compile or link time before the program gets shipped to a
            • 58:00 - 58:30 customer uh the bad news is that these error messages can be spectacularly bad uh my my best example recently was I had a seven line program that used one little um parameterized class and I was on a I was using Sig win on a Windows machine and I never figured out what the first few pages of the error messages was because
            • 58:30 - 59:00 it blew my uh Windows buffer uh s wind window buff um then I tried another compiler and I got the same result except that the error messages were totally different and it had nothing to do with the code I wrote that was in any way obvious so we really should deal with this so I'm trying first of all Let's see we we need what we have found is that people write compiled programs and they have really good reasons for it um mostly performance but
            • 59:00 - 59:30 also uh to express things as directly as possible uh why delay to runtime what you can do at at compile time so you get your errors and things like that so people have been doing this and so like any new feature that's sort of cool powerful um expressive efficient it gets overused and there's been serious overuse in particular people write
            • 59:30 - 60:00 template meta programming to compute values C++ 11 provides you the ability to write functions at that's executed at compile time which take care of some of the worst abuses of of templates um I'll get to other ways of improving that in a second but first I'll show those of you who haven't seen all that much C++ what the code looks like we can can parameterize uh data types with uh
            • 60:00 - 60:30 integers and uh we can parameterize algorithms with that so I can sort the vector of integers here and I can find the uh find my hometown in a list of um of of uh strings and the usual way we deal with uh returning a failure is to return the end if I'm looking through a list for something and I found the end
            • 60:30 - 61:00 instead of uh what I was looking for that's the way it is so we can write code like this we parameterize an element type and container type uh I could also do the find in the vector of integers so it's it's generic code and that's part of the the appeal of this um we can also parameterize on on criterias actions algorithms so here I want to have have a find something that not not really is equal to something but has a
            • 61:00 - 61:30 property here we write uh a predicate is is it less than Griffin and it's strings so that's that's meaningful uh and basically that's is done by doing what's known as function object you write a little class that can hold state so that you use in your operation and when you can construct it of course of course everything as Constructors and uh there is an application operator that is
            • 61:30 - 62:00 invoked to call it and it'll do something this case it compares to S and of course there's a Destructor here to clean up that string that we gave it it has to disappear and so you can write code like this once you have the less than and this people find it a bit of a nuisance to write all of that it's all of five lines but uh sometimes that's too much for people so you can use what's called a Lambda expression that
            • 62:00 - 62:30 basically writes the code for you so now we can uh compare using compare string compare to Griffin um lots of languages have these These Days okay um this is the code that that you write to implement this if you think C code is beautiful this is beautiful if you don't think it well this is what you got anyway and uh it very closely models
            • 62:30 - 63:00 what is being done in the machine so uh and it generates very nice code whether it is a list or a vector or a u c array or something like that so this is the way the code is done and uh if you get tired of writing those sequences uh like where where did it have a sequence yeah from the beginning to the end that's the way it's expressed uh and that's the most general form because now
            • 63:00 - 63:30 you can sort half a sequence or quar of a sequence and things like that it composes very nicely but it is a nuisance because the most common case is to do a whole sequence so you can do this uh sort the sequence using the predicate p and then I simply say use the standard Source from the beginning to the end using p is a predicate so you can easily uh write a simpler form of
            • 63:30 - 64:00 code so that your code shrinks it will still execute at exactly the same speed so um those of you who are used to C++ will know this is straight code so that's not a problem but basically um we we're working on the third part of the definition of templates how do we specify the interface to a template how do we specify the requirements that a template has on its arguments and the
            • 64:00 - 64:30 way we we're doing it we're saying doc typing just isn't enough we want to State the intent in other words we want to specify a template's requirements on its arguments and basically the programmer always knows that I mean you can't get work in code of that's templatized unless you have a good idea about what requirements there are what is this what are the template the actual template argument supposed to do for you I mean if I'm
            • 64:30 - 65:00 having a matrix of numbers I want numbers if I'm having a vector of elements they have to have the property of an element an element can be copied you can take its address and things like that the number is something you can add multiply or something like that so you know it already it's just there's been no way of saying it so what we can say here in a container for sort I want my the type container C
            • 65:00 - 65:30 to be sortable and what's sortable it's a predicate it's a predicate you apply it's either true or false for a given type I apply sortable to a vector and it will say something like H do you have random X is is is just something you can do random X's to and do you have an element type and can the elements of that type be compared um in this case using less than which is the default so that's what the sortable
            • 65:30 - 66:00 uh does and it will if you look it up be about three lines of code that says in code just what I uh do and today I actually uh write code in C++ 11 I put a comment there and I use this simply as my uh shorthand for writing good requirements I have a set of uh Concepts so I can uh provide the um the the specification in a concise way and in
            • 66:00 - 66:30 C++ 14 the concepts that are coming there this will be code so um we can also have some some notation here uh if I want a sort sortable it means that I want I can write it's a sortable type S that you're sorting which means that there has to be a type that is sortable and I pass that so it's just a short hand and you can use that for lambdas too I can say I want the Lambda that work for sortable C so container C so it
            • 66:30 - 67:00 all works out and now uh let's see how it works I have here the template of sortable container that's fine it's not quite the densest notation we've got but it's pretty there should be a type that's sortable and that's the type of my container I make myself a vector of doubles I W make myself a list of integers I sort the vector everything's fine what happens here is that it says
            • 67:00 - 67:30 hm is Vector of double a container yeah I can have random a I can uh have random access it has an element type I can compare it's fine sort the list is it a random access uh iterator no it's not it it you only have uh uh you can only search through a list you can't access the nth element directly so the actual error message coming out here
            • 67:30 - 68:00 list of integer does not satisfy the constraint of sortable so those of you who have suffered from the template definitions of the last 15 years I'm sorry I didn't know how to do that then now we do um there is an implementation of this stuff it's a branch of GCC look up Concepts Andrew S and you can find it and try and play with it uh if you switch if you throw one of the compile switches it'll then tell you why list of
            • 68:00 - 68:30 in does not satisfy sortable and it'll come back and says uh list of in is not sortable because it doesn't have a subscript operator that's simple uh the if you get the extra information will be about four lines as opposed to pages and add yeah that's what I said so uh back to to something that uh seems obvious but certainly is not what has been obvious in the C C++ and many
            • 68:30 - 69:00 other communities for a long time generic programming is just programming um there people develop strange theories and complicated theories about how you write templates and you use a different kind of logic when you're writing template programming and people invent very complicated ways of doing it but let's look at some code here is a square root function like we learned learned about the first day of our um programming uh when we learned
            • 69:00 - 69:30 programming right we can give it something that can be used as a double like an integer and everything's fine and the function gets called it Tak and gives you back the square root of um of X and um assuming that you know what a square root is you don't have to look into the implementation it just get called if you give a square root of something that isn't a double can't be made into a double like the address of a
            • 69:30 - 70:00 double you get an error message that says square root once a double now let's try that with a container we take a container we give it to the container and it says everything's fine because a vector of string is a container and it's sortable and whatever uh it works give it something that's not a vector it says no um sort wants a vector and the address of a vector is not a vector so we can get back to a a world
            • 70:00 - 70:30 where the code actually looks very similar whether it is generic or not and I don't think we have to learn significantly new Concepts uh we just have to get back to to to what we learned about on the first day of our programming course um writing the templates is a little bit harder but not much harder and it's significantly easier than it it is today um this this lets me into one of
            • 70:30 - 71:00 my favorite rats which is that uh the distinction uh between these so-called paradigms are more or less a function of uh the language features people had available and people have huge discussions about should you do conventional sea style programming do you use uh objectoriented programming putting things in hierarchies or should you parameterize and use generic programming um I think this is a function of uh of of poor uh language
            • 71:00 - 71:30 support and the um the the the discussions do harm so here is the classical example of um of an object-oriented programming written in a a slightly different style so I am drawing all containers for which the value type is shape and I do that by calling the for each uh algorithm for the container using the Lambda that
            • 71:30 - 72:00 draws the shapes um my point here is not it may not be your ideal draw all but it's it's pretty good I I can actually do it slightly better but this is this this is to make the point obviously this is objectoriented code I'm doing runtime polymorphism on a class hierarchy of shapes um is it generic code obviously that container is a parameterized type
            • 72:00 - 72:30 and actually draw all is parameterized on the container type and the element type of the container you go in here this is perfectly ordinary code it uh called a for each algorithm uh on a container using a Lambda uh we're certainly borrowing uh styles from the functional programming world where they have been very useful so uh do me a favor don't get into language Wars or paradigms I think the word
            • 72:30 - 73:00 Paradigm was seriously Mis defined by Kon when he first came it he came with it it was this idea that there is a paradigm then you get another Paradigm the first one gets totally useless shouldn't be used anymore the old guy should die off and the world would be much better for it um he used as an example Newton's world and Einstein's world but you know we still live in Newton's world most of the time and most of the calculations defined by Newton
            • 73:00 - 73:30 still works yes you need gravitational uh Einstein logic to deal with the high Precision GPS but that's very rare similarly uh CHR nugo who invented object-oriented programming U had the same kind of attitude to Paradigm he he used to talk about the old days we had plus and minus and that was what we had and then somebody invented multiplication and division now this is a new paradigm so clearly you have to
            • 73:30 - 74:00 get rid of the old Paradigm no more plus and minus that's oldfashioned and you can write everything you like with um with um with multiply and divide well I do wonder how people get negative numbers in that world but apart from that I think it's absurd the old ways sometimes are still simply absorbed in the new stuff and we can do more and we can do it better so that's it so challenges uh
            • 74:00 - 74:30 basically uh I'm not claiming C++ is is is perfect we have to see what we we can do to make it better uh we have to make programmers actually prefer modern code for um the kind of applications the kind of things C++ is good at so I want the code like I showed you to be used on close to the hardware high performance uh code because it works and it works better than uh the
            • 74:30 - 75:00 old code the unique pointer runs exactly as fast so the last cycle that well written code with a raw pointer and proper deletes the sort algorithm here blows away a CQ CQ sord by factors not percent so you can afford it but we have to convince people to to do it and then we need to make C++ a better language
            • 75:00 - 75:30 under the constraints that it still has to be the best language for uh really lowlevel code and abstraction above that that's that's the aim and uh we we uh that's improve and we have compatibility performance portability range of application areas we cannot improve the language just by making it a a domain specific langu language that that again would would violate some of the ground rules and uh there's some more
            • 75:30 - 76:00 information there's a new website more or less new website isoc cp.org that will offer you uh access to a variety of informations including about the standards and Etc stuff that floats by there are some books and a website by me I'm especially proud of this little book tour of C++ it explains all of C plus plus and 180 Pages um of course I have to leave out
            • 76:00 - 76:30 some details it's uh it's at a reasonably high level but there's lots and lots of code and so that's 180 uh page version that's what you can read on a long train or plane journey and the rest is details well there are uh let's see 1,300 pages of uh details in that book same number of details even more details in 1300 pages in The Standard and that's encrypted in
            • 76:30 - 77:00 standard D is to stop you from reading it um I I did not invent the rules for the ISO standards but they they can be pretty gross but if you want to know everything about C++ search for wg21 which is the official name of the standards committee I told you standard e encrypts everything including the name of the standards committee uh and you can find the stand you can find every committee do document it's it's public and available and then basically I will
            • 77:00 - 77:30 switch to questions and answers thank you so I'm sure after that Master Class there'll be just one or two questions um it can be anything from a a technical question or something broader more
            • 77:30 - 78:00 general just about yarn's experiences in in general so if I can throw it out to you and we've got a couple of roving mics we'll get to you as quickly as possible if you can introduce yourself and fire ER your question one of the things you put next to the um C++ elephant was buffer overflow and in the wake of heart bleed I saw a tweet circulating around which said every security bug you've ever ever faced was Dennis Richie's fault so I want to say is do you want to
            • 78:00 - 78:30 claim any of the credit and what do you think the way forward is to avoid things like heart blade yeah let let's say it this way we shouldn't be rude to Dennis Richie he was the guy who more than anybody else lifted the level of programming from ass simply code to a higher level notation and for that he deserves a lot of credit and if we have to blame somebody it should be the people who still write code as Dennis Richie did in
            • 78:30 - 79:00 1973 um now I haven't had a buos flow in a long time and the reason is that I actually only use the cras uh directly if I am doing a hardware uh operating system load and store everything else I use a vector and I have the size sitting right there if I
            • 79:00 - 79:30 write a loop the easiest way to write the code is to ask for the size and use it and for that reason buffer overflows doesn't really happen in my code anymore and if people used vectors and containers and such that would be very very common it's a similar thing that uh I could have put put uh memory um I mean SEC for or something
            • 79:30 - 80:00 on that list um and again it it just doesn't happen unless you are using pointers in a in a fairly silly way and again you don't have to do that anymore you use the containers that has access operations uh you use the smart pointers so that they actually point to something as opposed to to nothing and so these problems are by and large gone um by and large is probably not uh
            • 80:00 - 80:30 good enough for for some people's taste but it's better than the the the the C model here um I know that processors from Intel at least will come with Hardware support for range checking um in I think next year it has been uh announced and I expect the C++ compilers will then take what is currently undefined which is accessing outside the array um and enforce it by Hardware so
            • 80:30 - 81:00 that um it's dirt sh um there's a uh you can you can do that and I would like to build a completely typ safe system around this at the runtime speeds we've got uh I think it can be done provided you provide a loophole when you're accessing hardware and operating system resources directly and and and
            • 81:00 - 81:30 basically declaring that some code is trusted and beyond that you can be fully C safe I haven't done it yet so it hasn't been proven but for me personally that problem is in the past by several years and it could be in yours too if if you can move to the modern world Eric thus University better it seems that um one of the hardest things about C++ is accessibility to uh
            • 81:30 - 82:00 large projects for new developers so if you're a new developer and you approach a project with lots of meta programming with uh templates inheriting from classes iner inheriting from templates and so forth it's really hard to figure out the flow of execution through the program and the way the data moves through the program uh so first question do you also think this is a problem and second question do you have any ideas of helping new developers get into a project yeah C++ is obviously too
            • 82:00 - 82:30 complex and it has all these Barnacles from its history and if you get a project where people have gone Hog Wild on um on templates or on class hierarchies or on using CP pointers you have a major problem on your hands um you can write bad code in in lots of languages and then C+ Plus+ you can even do it in many separate ways um I think there's a major problem in
            • 82:30 - 83:00 the education where people a lot of professors insist teaching you the bad ways of doing things first and a lot of them never gets to the good ways because after all they had to suffer to learn C so why shouldn't you uh probably builds character uh so I think partly it's an education system the education problem and I'm trying to address that with some of the books I shown partly it's the
            • 83:00 - 83:30 fact that if you are attacking a million line um code base uh it's going to be have been written over years it's going to have different styles in it um we need better tools for upgrading and for understanding it it's it's actually coming especially in the clang world where you can where they have pretty good static analyzers coar has been doing it for a while but a lot of people
            • 83:30 - 84:00 don't use them because it cost money they rather waste their own time and money than than pay others for solving problems um and uh there will be much more coming out of clang later this year I know so I have some hope that static analysis will help you and that um that static that code transformation will will become common to simplify code now for the specific problem of our template meta programming I have been encouraging
            • 84:00 - 84:30 people all the time to not do things as complicated as they can there's the old saying if you're writing the code as complicated as you can handle you can't maintain it because you just did the best you could and now you have to find the bug so my guess is that if you really have the truly horrendous template meta programming you should probably back it out and with Concepts
            • 84:30 - 85:00 you can eliminate some template meta program by simply writing them simpler and by Conex functions you can eliminate essentially every template meta programming that generates a value by writing ordinary code so yeah I think there's another question somewhere up here hi uh my name is Dan um I work at Neo uh
            • 85:00 - 85:30 my question is about uh compile times one of the things I've heard people say they suffer with but with c and C++ is fairly long compile times and I believe one of the things that was cited as a potential reason for it was uh repeated rereading of headers things like that so I wondered do you can you see if there if is there anything that you think could be done in terms of language design rather uh improvements
            • 85:30 - 86:00 rather than just compiler optimization that could improve the situation um that's a very tricky question that we have been trying to deal with for years um there are languages that are even slower at compiling but I'll be polite and not mention them uh on the other hand we all suffer from long compile times and there actually is work going on doing that
            • 86:00 - 86:30 with um a fairly simple module system put in place so that you don't do repeated uh compiles of the same header file I mean if you include the same header file in 100 uh source files you get to compile it 100 times and there are people that do much worse than that um it is being developed primarily at Apple and
            • 86:30 - 87:00 Google it is in use uh for C Objective C and C++ um I expect to hear much more about it quite soon there is a study group in the standards committee called modules where you can go and look at at what they've got they're not very good at publishing early but there are documents that describe it I expect to see um at
            • 87:00 - 87:30 least initial beta releases widely available uh next year the two organizations I mentioned are not the only one that is interested and are not the only ones that are working on that so it will not be solved for C++ 14 but I expect to it to be solved by C++ 17 and be a aailable uh in major compilers notably clang um much sooner than that
            • 87:30 - 88:00 do you have maybe some statistics or do you know about the trends for programmers moving from older standards to C++ 11 or maybe the newer one how eager they are to do that because sorry uh like C++ 11 it's here for a few years already and I haven't been reading a lot of the code that's written in that standard I just read some older standards code one of
            • 88:00 - 88:30 the problems I listed on the challenges is to get people to upgrade and to get them to understand it and there are lot of organizations that has code bases that should be compilable by old uh compilers and therefore they can't move forward this is very sad and we have to do something about it I think what we have to do is to point out that the newer coding styles are safer and
            • 88:30 - 89:00 actually generate faster code and then we have to back this observation with code analysis and uh code transformation to rejuvenate code or modernize code whatever you call it uh that said there's actually a lot of C++ code around there there's even C++ 14 code shipping in products it's mostly in organization ations that has a control of their code base that is have a unified code base have a unified checkin
            • 89:00 - 89:30 procedur supposed to be fragmented uh through many user uh groups and uh so you you find code something like Google being far more modern than than the impression you're getting uh the adoption of C++ 11 has been significantly faster than the adoption was of C++ 98 even though the problem of moving ahead is obviously larger now because
            • 89:30 - 90:00 there are more old compilers and more code I mean we're talking about billions of lines of code here I'm sure uh you could spend the whole working life never getting out of some of the older Styles similarly you could spend your whole working life from now on never seeing any of the older stuff um it's a huge community hello Vladimir University of Edinburgh
            • 90:00 - 90:30 my question is the following um plain old C is pretty good at maintaining binary compatibility with various modules however in C++ that's a bit more challenging and moduls compiled with different compilers um require a bit more work to make them work together do you think the current situation is okay or does it need any Improvement it would be it would be really nice if you could mix and
            • 90:30 - 91:00 match code compiled with any compiler of any vintage of C++ uh you can't uh the origin of this problem is that every vendor in the 80s and early 90s thought they were going to dominate the world and everybody would agree to using their linkage standards and their object format and every vendor uh tried to improve their uh linkage formats and
            • 91:00 - 91:30 their compilers and therefore they they couldn't agree on on a standard there was an attempt the itanium ABI was adopted in some cases um there are some hope for better linkage for the uh 64-bit architectures but it is a real problem um think about it this way if you you are a compiler vendor with say a billion lines of user code out there from your
            • 91:30 - 92:00 customers what are you going to be compatible with the other guys compiler or the compiler that you shipped last year as far as I can see every vendor be it open source or not chooses to maintain compatibility with their previous compiler to the largest possible extent and when they break compatibility they don't don't actually quickly move to something that's compatible with something else they put in some new optimizations that that
            • 92:00 - 92:30 suits them better so it is a problem C's compatibility is almost a historical accident because there was um compilers that was popular on different platforms and people imitated them with C++ when they put in a C++ compiler they competed so I've seen examples where one compiler takes all the implementation defined things and does this to them and the next one comes along look at the list and do the exact opposite in almost all
            • 92:30 - 93:00 the cases um linkage is not standardized it's not standardized by for C it's not standardized for C++ it is a real real problem and it's a nuisance and I don't know what to do about it hi um my name is Andrew uh I work at Skyscanner um you spoke about uh the Barnacles on C++ to maintain backwards compatibility
            • 93:00 - 93:30 um have you ever considered writing a completely new language from the ground up um using Concepts that you want in C++ but might not necessarily be able to put in uh and if you did do you think you'd end up with Java the the second part of that question is very easy to answer no I wouldn't build something that looked anyway like Java it it totally over uh
            • 93:30 - 94:00 emphasizes object-oriented programming and hierarchies and it uses a garbage collector and it uses a virtual memory it's in the way of everything I like about a systems programming language um it's probably fulfilling its own design criteria very well uh C++ doesn't fulfill the uh Java criteria either uh now of course I've dreamt of
            • 94:00 - 94:30 building a new uh programming language with a nicer syntax nicer semantics nicer properties etc etc etc uh my first guess is that unless you could embed it in the C++ community and infrastructure it would fail very fast so it's not an easy thing to to do and not the only I'm not the only one who has wanted to do something like that
            • 94:30 - 95:00 but hi um very hard a question from up to your right sorry um just one question we see more yeah we see more system languages on the hardware definition side attempt well we see Hardware definition languages trying to move up to system level um C++ is obviously operating close to the metal do you see C++ moving downwards towards the hardware
            • 95:00 - 95:30 definition languages at all or do you think do you see the hardware definition languages moving up toward C++ um I don't know enough about that particular area to be really certain but I think C+ plus makes a rather nice um Hardware definition language which especially if you use uh templates in a fairly moderate uh way that way you can
            • 95:30 - 96:00 write things that that well anyway I I won't go further than that um I I don't know enough to to be sure okay yeah I had another question about Barnacles so of the 1300 pages in The C++ specification um about how many pages would would you say are barnacles and which ones that's hard uh let me just point
            • 96:00 - 96:30 out by the way that 1300 pages is also the size of the C specification and of the Java specification plusus 5% so it is not specific to C++ uh Barnacles um there's some really weird side effect to see uh in it and places um certainly I would like the rules for uh say conversions not to go both ways I
            • 96:30 - 97:00 mean it's fine to assign a character to a long it is really nice to assign along long to a um Char though in real code obviously people have tested it so most of the time there's not a problem in existing code but the type system gets seriously Complicated by that kind of of stuff uh similarly um I I don't actually it's nice to have a data type you can do bit
            • 97:00 - 97:30 operations some and and all but but why should that double as an integer um and I really like arrays for dealing with the lowest level of Hardware I don't know how to write a systems programming language that doesn't have something like an array but why should you be allowed to forget its size and and why should it convert to a pointer of potentially another type when you're when you're going into class hierarchies
            • 97:30 - 98:00 no so these are sort of the worst barles then there's some syntactic audities and so but let let me not go to a through a whole list of these things because they're there we learn we have been I mean C++ when you take into account C is more than 40 years old so you expect some some Barnacles and we've tried many times to get rid of them deprecating features uh options for
            • 98:00 - 98:30 for not doing things doesn't work the community when given a choice between getting rid of dangerous old features and not breaking their code chooses not breaking their codes if you get an oracle C++ compiler it will come with a minus arm option which says follow the rules of my draft document for starting the standoffs process from
            • 98:30 - 99:00 1988 a lot has happened since then but they have not been able to remove that feature because they have paying customers that insist on it but if I could just ask maybe um if we could all show our appreciation and thank you very much for the effort and the amount of time you you [Music] can
            • 99:00 - 99:30 this production is brought to you by the University of Edinburgh