C++ - the Newest Old Language β€’ Matt Godbolt β€’ GOTO 2018

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

FYI, here's the talk Abstract

C++ is an old language only used by the unfortunate few that absolutely have to, right?

Wrong!

C++ is not the language it used to be: clunky, error-prone and lacking in tooling. A lot has changed in the last decade: three new language revisions, new compilers and all-new tooling and diagnostics. Memory leaks and segmentation faults are all but a distant memory!

In this talk, Matt will cover some of the things that make C++ a great choice for a wide variety of applications. He'll cover the best of the new bits of the language, and demonstrate how modern tooling makes coding in C++ fun again.

The intended audience is both C++ folks who might want to catch up on the latest and greatest, but also anyone who think C++ is a write-off and isn't used any more.

πŸ‘οΈŽ︎ 56 πŸ‘€οΈŽ︎ u/goto-con πŸ“…οΈŽ︎ Sep 25 2019 πŸ—«︎ replies

The first audience question at the end was "are you aware of Rust?"

I think a talk about "why use C++" would be nice? Because whenever you mention C++ to a non-C++ programmer, the end of the conversation leads to Rust most of the time. The new C++ features are answered with "I already had those for quite a while now." Once you get to systems programming and performance, the answer is "there's Rust for that."

So, really, we need "why use C++" talks.

πŸ‘οΈŽ︎ 31 πŸ‘€οΈŽ︎ u/RealNC πŸ“…οΈŽ︎ Sep 25 2019 πŸ—«︎ replies

This is the newest old video

πŸ‘οΈŽ︎ 7 πŸ‘€οΈŽ︎ u/[deleted] πŸ“…οΈŽ︎ Sep 25 2019 πŸ—«︎ replies

such a tease, thought it was one of his cppcon2019 vids

πŸ‘οΈŽ︎ 6 πŸ‘€οΈŽ︎ u/puhniste πŸ“…οΈŽ︎ Sep 25 2019 πŸ—«︎ replies
Captions
[Music] I haven't even done anything yet I getting applause this is a great start to the day yeah good okay so there's this great session thing like Jonathan says please rate at the end of the talk and yeah ask me questions there and I'll hopefully have about 10 minutes of the enfant for questions but we all know how that goes right so I am today I'm going to talk to you about C++ it's the oldest new language this is sort of rather pejoratively called the old-school languages but I would like to sort of try and leave you with the impression that C++ is not a funny old language it is in fact a cutting-edge awesome language that you should all be using so who uses C++ first of all of you lot one person using C++ anyone else using C++ currently two three yeah some people Wow okay this is useful because it tells me whether or not I should go more into the details of some of the watery bits of C++ or whether I should concentrate on the the bits that are interesting to me about choosing C++ as a language so in answer to who uses C++ more generally like industry side of things and I'm sorry people are waving me from outside I've used C++ pretty much exclusively through my my career and I spent 10 years in the games industry and in the games industry C++ is a pretty good choice for writing engines and 3d systems back at the very very beginning like the late 90s in my career it was still C rather than C++ but C++ became pretty much the standard after that obviously scripting within games is done in other languages sometimes lower and other bits and pieces but C++ is a good good place to start I've worked in finance and finance certainly if you're doing anything that has like a high throughput or a low latency flavor to it C++ can be a good choice there too embedded devices again C is also a good choice for these things but C++ is becoming more and more of a good choice because it gives you the flexibility of a high level language and all the con trucks that come with that were but still be able to get down and poke hardware registers as you might need to be able to do and I've played around on cell phones writing MPEG decoders and things like that and C++ was a great choice for that kind of thing and talking to like hardware MPEG devices and in servers if you're you know if you type a web query right now it's almost certainly going to be handled by something written in C++ on the back end but but why did why would he pick C++ so obviously the first thing that springs to mind for most people about C++ is that it's something you write in if you want high-performance code and I'm gonna make no apologies this is an evangelical talk about C++ other languages are available and and in fact I will make reference to them but I'm not an expert in other languages so I will try and keep my my mentions of other languages too factual and not sort of emotional because I'm certain there are people in this audience who know a lot more about those languages than I so I'm gonna surprise you the things about C++ that I think make it a good choice are these value types clear object lifetimes and then third performance so performance obviously is a very important thing for a lot of the applications I've just described but there are aspects of C++ that make it a really good choice even if you don't need to be writing super performant code I'm going to come back to these in a few slides and explain a little bit more about what I mean by a value type and what the clear object lifetimes mean but by now somebody in this audience possibly all of you are thinking but isn't C++ that horrible thing with the multiple pages of error messages and all the crashes and the memory leaks and and all the security flaws and all that stuff and of course yes to some extent it was that's the old C++ new C++ is better and of course because it's backwards compatible you can still write old-style C++ and leave yourself open to all these problems but I'm going to hopefully explain to you that you shouldn't be doing that anymore and that there is a better choice for you so what has changed in the language I only give a little table here and give you the highlights of each revision of the language many of you may not know that C++ has been room updated fairly frequently well I guess we started in 1998 with like the first real version of C++ that was a standard and the highlights of that were memory leaks use after free crashes all those things now obviously those are not really highlights but I want to try make a point here and then in 2011 a new standard for C++ was released there was a committee meeting in the late 2000s and for those of you who who know about these things it was called C++ Oh x4 forever because they thought they were actually going to get it out in the 2000s but it sort of over on and over ran and eventually became C++ 11 so there you go but we got a whole raft of awesome new features the first thing is this keyword auto now if you're as old as me you may know that auto has always been a keyword in C and it's like the opposite of static and it's also the default so you would never write int I as a so you're never an auto int I semicolon till I define a variable called I but that's what it means the auto means it's automatic storage it means it's on on the stack but nobody used that so they've co-opted the keyword and auto now means you guess the type compiler so if I do Auto I equals zero semicolon eyes type will be different derives to be an integer and if I do Auto I equals false then I will be boolean or if I call some complicated function it will be whatever that complicated function returns and that's super handy you've probably if you're like you used to programming in less strongly typed languages you're used to things like let or other sort of ways to just say I just need a variable called X and it's this thing over here C++ is not weakly typed so there is a strong type associated with that but the auto says you work it out compiler you figure it out for me and it gets rid of so much of the boilerplate code that you would otherwise need I've got some examples of that coming up we've got range for so before this if you wanted to iterate over a container or a sequence of things you'd either have to count from zero to the number of things in the container or you'd have to use these complicated iterator trait type things where it's like vector of int Colin : const iterator and that's already a huge amount to type out and you start at the beginning and you keep iterating until you hit the end and that's just very inconvenient so we get range four so now I can go over a container by going for auto I : container and I will get for every element in that container I get a variable called I and I didn't even have to know what what type it was because the auto thing has worked that out for me too we get lambdas so I can now define a small function inline where I need it and those lambdas can capture variables by either copy or reference from there enclosing scope and that's a really powerful thing if you're writing functional style programming where you might have an algorithm that says I need to combine a bunch of things together and I'd like to supply the combining function now previously you'd have to define somewhere else a function that is the how to combine things function and you'd have to refer to it in the program at the point where you were were calling it but now you can actually just define in that spot the function itself and even more importantly you can capture some like local variables or state basically a closure we got move which I've just put down as move him and there's a whole bunch of machinery that was added to the language to support the idea that object lifetimes can be transferred so oh through object ownership can be transferred so I can clearly define when I'm giving an object to somebody else and I don't need it anymore and that's really important with the next point which is smart pointers I can write and if the standard now provides a set of template objects which look like pointers but under the hood they actively manage the thing that they're pointing at so whereas before you might just do int star I equals new int and then just remember to delete I when you finished with it else you've got a memory leak I can now make a unique pointer to I and a unique pointer says I uniquely owned the thing that I'm pointing at and when the unique pointer falls away out of scope and can't be or by the compiler it will be its destructor will run and its destructor will clean up the piece of memory that it's pointing at and that's extremely powerful it means that I can effectively trust the compiler to generate both the [Music] what would to capture the pointer that I have created into the heap and also to guarantee that it will be thrown away when I finished with it that's really really powerful but in order to be able to use that effectively I need to be able to say well I've made one of these things and I'd like to give it to somebody else now but the uniqueness of unique pointer says that I can't have a copy of it there I'm not allowed to copy these things because it would be a unique anymore either have to clone the object is pointing at which would be an expensive operation or there would have to be two pointers pointing at the same object and then I'd have to remember that only one of them is allowed to destroy and gets complicated so the move semantics allowed me to wrap an object and say hey I'm giving this to you now and my object is dead that's really powerful we also got this thing called context / context / allows us to write compile time run code or in principle compile time run code and this is a new laptop and I haven't chest set it up for conferences yeah means I've been talking about this for too long if the saver kicks in so I can write compile time code and if you've ever played around with like preprocessor tricks because you need like a constant that's the available at compile time then now you could write it without horrible macros you can write it in the language you're already writing in will we'll see a little bit of that down the line but that's a pretty powerful concept and it's going to be built on in future we've got Atomics atomic sir a lettuce access memory in a way that is thread multiple threads safe if you if you're careful we get a memory model that allows threading to actually be well defined we've got user-defined literals that there's tons of stuff it was a huge reboot to the language and in three years later we've got another revision of the language giving us a whole bunch of stuff including return type deductions so now I don't even need to say what my function returns I can just do Auto myfunc and then just do return seven it's like well I guess you wanted it this to return and into which obviously saves on typing but in some generic code that's a very valuable thing I don't maybe I don't know what the result of what I'm going to return is because it's based on what I was given so it's a powerful concept and the lambda support got even better so now we can actually capture things and move them into the lambdas we can rename them we can do some bits and pieces that allow me to more effectively use lambdas and Landers became what themselves context borough as well which is just another mind-bending thing I can write generic code using lambdas and know that they can be in principle executed at compile time and the result just being fed out and might just be a number so that's pretty powerful and I have a friend of mine has done a presentation where they've written he's written a compile time JSON parser which is just nuts anyway unimportant using lambdas C++ 17 this is the most recent version of the the standard we've got if Const X burrow which is essentially an if statement that's run by the compiler with a constant expression something which can be evaluated at compile time and it works a bit like the hash if if you've done again preprocessor hacks before now so I can have a piece of code that's gent generic and maybe take some kind of type T that I don't know about say a template type T and then I can actually have conditional bits of code that say well if T is an int then I'm gonna do this thing and if T is a double I can do this other thing and the important thing is that the stuff that's in the if statement just needs to be possible it doesn't have to be semantically correct so if the failing case if the else clause that wasn't actually run by my if it's not well formed for whatever type it is maybe it's like you know they say if T is a pointer type then dereference it now of course if T wasn't a pointer type that wouldn't be valid code the compiler is allowed to throw that away now and it gives us a powerful way for writing template meta programming sort of very high order functions without resorting to resorting to some of the old techniques that you probably have seen along the way that give the voluminous error messages that I alluded to at the beginning we also got a whole bunch of new things in the standard some algebraic data types optional variant there's an any type that could hold anything string view is like a window into an existing area that Alexis like look at a sequence of bytes as a string without like copying it and these are things are very very powerful so these we've got a lot of cool things that come down the pylons what I'm trying to say here and in C++ 20 which is already be in the pipeline we're gonna get concepts hopefully we're gonna get some ranges which allow us to do like almost like pipe based programming you heard at the keynote like UNIX pipes are a forgotten thing and I disagree for that I used buicks pipes all the time and it would be even more awesome to be able to take a range of values pieden through something which doubles them and pipe them through something which sorts them out pipe them through something else and then get get a range out of the end and say well this is like a my lazy evaluation of things that so that's hopefully coming co-routines as well which we may network programming more palatable hopefully and these things are already starting to appear in compilers which is amazing so I want to just give you a quick example of how code might look different I don't know if that's they is that readable enough I don't know if I can do much about the font size so I apologize in advance and I'll go through this super quickly I'm not gonna try and explain because I've already ascertained that most of the room is not currently doing C++ development but this is like an old way of iterating over an array the main the main sort of aspect here is this horrific variable I had to define that says you know this is the thing this is the type of the thing that I'm iterating with and that's just horrible so the first thing we could do is just replace it with Auto which is lovely so I don't have to remember what my silly type is or how many colons or less-than or greater-than as I need but even better I don't need to iterate over this anymore I can use the range for so then it just becomes that much nicer of course if you were a functional programmer type person you might know that there is a built-in function to C++ called accumulate which allows you to say well here's the beginning of something that you can iterate over here's the end of something that I want you to wait right over here's the starting value just add them all up or accumulate them all in which case you could write something like this so I mentioned earlier about lambdas being allowing you to write stuff in line this is the opposite of that there's my function at the top there that's accumulating and in this instance because I'm doing a root mean square sorry I didn't say that earlier I need to take the partial sum and add the square of the thing that's being to it to kind of get my sum of the squares before I square root it so I mean it's not too bad in this case but if you've got a big sprawling codebase having like loads of functions everywhere it's just you know cognitively loading so you can write this which arguably isn't much better all things considered but at least I've got my begin and end by zero and then there is my lambda this block in the middle there's just says hey I just need a function right here that does this please so things have got a little bit better there I hope so C++ has changed C++ has evolved a lot and some of the things like the smart pointers are sort of game changes you no longer have to worry about use after free or or like memory leaks and various things like that you if you can rely totally on smart pointers to manage the memory for you then you're in a good place already but why would you pick C++ when there are so many other good languages out there I mean in particular like things like rust are a really good choices for this and I'm very excited about rust and I think probably maybe I'm speaking out of turn here but I wonder if a lot of the acceleration in C++ is development has come from like the fact that there is now a genuine competitor this is C++ so I want to talk a little bit about why I think those value types the first thing that I said am i bullet lists are really really important so what is a value type it's passed an object that's passed by value I'm not gonna take reference to this this to this thing it's like either in just an integer or a pair of integers or something that's small maybe you know 16 32 bytes worth of data for which it is cheaper just to copy around by value rather than having something in memory somewhere that I keep referring back to and if you come from like manage languages like Java in particular it's hard to think about anything that isn't a reference because every object that you've ever dealt with is a new dub object that lives on the heap somewhere that you're pointing to but there's a sort of category of problems that come with reference based programming and that is you don't know who else has got a reference to that object and you it's very hard for you to reason about whether or that object is going to change under your feet so if you've got a function that takes a ref to an object and maybe you have some kind of listen a callback thing and you like your write writing your code and your function runs you do some work and then you say oh I admit my something interesting happened event and I know somebody subscribed to it they've done something with it and then I carry on with my code for all you know all your references have gone unchanged to be something else you know that the object pointed up has been mutated in some way and that's that makes it difficult to reason about so passing by value gets rid of that you've got your own copy of that that's really cool in C++ value types have no overhead at all they are literally the four or eight or sixteen bytes that are in a register somewhere or on the stack somewhere and that means that there is just no overhead I'm passing an inter and but it's not an int it's it's my type and my type can have behavior so I can define what it means to add two of my types together it can have I can write streaming operators for it I can I can sort of make it my own knowing that really it's still just a 32-bit value that's going to be treated as an integer somewhere and they're really useful for things like positions error codes prices quantities times anything where it would be really really really bad if you got things mixed up I mean I saw I worked in finance for a very long time and you never want to get your parameters the one way round to your function which I'll show you in a second and you know it's just there's a lot more what's the word oh gosh I had a good word for this and my brains gone blank not enough coffee you know you're modeling your domain by saying that this is a type of this rather than just saying into something in your in your function signature you say no it's a quantity it's like oh that's that's handy I'm documenting it's it's and so that C++ is strong typing means that I will get an error if for example haha if I were to write a function called buy shares and it takes a con char pointer to the symbol it takes a double for the price and I'm gonna stop right here and say never ever ever use doubles for prices this is purely here a slide code I will be lynched if anyone ever sees sees this in production code and an in quantity there and then I'm gonna call my function with buy shares want to buy eight hundred shares of Google for nine hundred and seventy five bucks right and the compiler will let me do this and in very many cases it even give me a warning that something has gone on because whoops I got the parameters the wrong way around 800 is a completely valid double precision number and 970 5.0 can be pretty easily coerced into an integer and you know microsoft's compiler I think wins is that you're like narrowing here with the warning level cranked up on clang you'll get some of the warnings here but it's like it's it's the kind of thing you could imagine yourself doing right and it's the kind of thing that could be very very expensive to get the wrong way around so what if instead I'd written it this way I've used my little tiny tights my price price in my quantity quantity knowing that they really still just a register value so I'm not giving up any performance here and then I try and do this still so I'm gonna buy 800 at 975 and now the compiler tells me that I've made a mistake and that's great and it's the the message is not the best right I mean I was trying to like I'm trying to spin this to be like C++ is awesome and all the met error messages that you see in your past have all been fixed but it's not quite true but that's not terrible it's telling me that like the second argument here I couldn't turn that quantity you gave me into a pricing like whoops that's an expensive mistake averted and so this is what a value type might look like it's written just like a regular class or a structure I'm just going to highlight some other new things that are new to C++ in the implementation of a value type this is a two-dimensional position using floating point values for the X and the y and so the first thing I say is like I can initialize my X&Y in my structure and again if you guys are not familiar with C++ this is like news to us I know Java has had it since forever and and the initializer here is the default initializer I've just used open and closed parens here which will mean it we initialize to zero but I could also just done float x equals zero or float x equals 10 or whatever I wanted the default value to be so that's pretty cool I'm gonna skip that and come back to it this is the constructor that says okay if you do want to make a position with a preformed position with an x and a y that i already know then you can construct one with an x and a y and I'm just going to initialize the X with the X and the y with the wide I would be past it that's pretty cool now as soon as I start defining my own constructors C++ says ok I get at you you want to do the construction yourself I won't allow anybody to make a position without using one of the constructors that you get me but I just went to all this attention this care to make sure that like it would be initialized to zero by default right so I want to kind of get that that behavior back I'm gonna say no no no the empty constructor the one that doesn't take any arguments at all that allows me to just go pause P that's just default bring that band back bring it back and it will be default initialized to x equals zero y equals zero and just as an example down here this is like how I start adding behavior I'm saying that this is how to add a position to me to give a new position so for those not familiar with C++ sort of idioms the context bird the beginning is one of the new C++ 11 features which says look in principle this could be done at compile time and if I were to do anything in here that was not in principle compile time doable if I were to allocate memory in here or to throw an exception on a path where it would actually be executed by the compiler it would the compiler would bug out and say no I can't do this I can't do this at compile time so it's completely unnecessary in this case I'm just going exposing it here I'm returning a pause object I'm the operator plus this is what happens that if I add I'm the other pause here is what I miss being added to me and you'll notice that I'm taking it by value this is the point of these things being value types I can guarantee that it'll fit in a register or two registers or whatever and it'll just come to me and be quick and I'm returning a new position using the constructor I've defined above just by adding things together with it and then the thing that I really want to draw your attention to is non C++ that this thingy doesn't work anyone be on the recording anyway is the Const at the end of that line there so the console extra pause operator plus poles other Const that is I think fairly unique to C++ maybe rust as well maybe anyone who's a language expert here it means that the object that this method is being called on is itself constant for the duration of this method I can't mutate the object here is it's a it's a big hint to me as a programmer that I'm not allowed to change myself and I'm telling also my callers that you can call my method and nothing interesting will happen to the object it will still be the same object afterwards that's that's I think a useful thing and something I miss from from when I do any kind of Java side work or stuff like that where where I get a method and I you know you'll get method you could potentially poke around inside your object and chain things so I koala that's a nice thing about seeing us and then he would do other all the other operators and you have a nice value type a quick example of the clear object lifetime that I talked about earlier so I mentioned that when an object falls out of scope its destructor is run and almost all languages have some notion of when my object is dead I'd like you to do something for me a Java has finalizes and c-sharp probably has something similar right I'm looking at the crowd in case anybody can correct me again I said I'm not an expert than anything other than well not even see the spaz but whatever so fine that the problem with finalizes is that they are run when the garbage collector or whatever mechanism is used to track when an object is no longer needed just realizes that that object is unreachable and that's great but may not be a good time for you so for example here I've made up a database query function here and this is the worst code is slide code and all that kind of nonsense but on this one here I'm this mythical DB connection object that it doesn't exist anywhere it's just made up but I'm constructing one and I'm giving it a hard-coded name of the server to connect to called server and port 9000 or whatever those parameters mean it doesn't really matter but you know that if you're talking to a database somewhere behind the scenes there's going to be probably a TCP connection so a sockets being created a connection has happened some probably some buffers have been allocated on your side and probably on the database now you're taking up resources on this remote database there's a connection up on the the database side that's that's taking up some of its RAM and it's like God its buffer and it's allocations on that side so that's an expensive heavyweight thing to do and obviously in a real code you'd cash it in some way and do connection pooling and also some whatever but okay it's like slide good and then I'm gonna call query and I'm gonna return the query so I'm gonna delegate out to whatever this query is going to do presumably it runs the query on the database and gives me a string of the result like the worst representation ever of a query but whatever and I'm just going to return it to my caller and that's great what about that database connection what about all the resources associated with it in a in a managed language you would probably have to do something where you explicitly call something too they like clothes conduct clothes or or you would have like a whizz block or some of the other sort of I'm only familiar with Python in this particular thing where you don't with open or foo as X and you indent the block and then at the closing of the block then some magic method is called on the the object that you whiz with and that's great but you have to remember to do it and this means I can write code database connection code like this which there's just no way to leave a dangling reference unless it as soon as that con object is not available to anybody anymore then that connection is closed the resources on my site are cleaned up and the resources and the remote side will be cleaned up as well and of course there's a whole bunch of things to do with Eric checking and exceptions and stuff so obviously but I get to make up crappy code like this and you have to kind of like invent the other bits for me another example now this is sort of showing the the transfer of ownership that I was talking about earlier is like this this example here where I have a document and a document has elements and the elements are like some virtual base class and that is an abstract class an interface class for people coming from co-op java worlds the problem with those objects is that i don't know how big they are because it's up to the caller the caller can make any implementation that conforms to that interface and they can make any object they like and give it to me as an them so i can't store a lens myself directly so i can't just have an array of them and i can't just have a vector of the money to have references to them on the heap and again as soon as you start having references to things on the heap you have to be careful about whose responsibility is to clean up those references those objects so here what I've done is I've stored my I'm gonna store my lens if my clicker works in a vector which is to say a growing array of unique pointers to a lens so the unique pointer is going to be the guy who's going to look after the allocation and de-allocation or rather the deallocation in this case of those LM objects the problem is though those unique pointers can't be copied because again they're unique so how do we how do we how do we get things into them in the first place because someone has one and they're going to give me their Blair their pointer and then I have to kind of well I need to get it in somehow without copying it and the way to do that is with this double ampersand thing here now I will say that that's not a very good syntax for anybody but this double ampersand here is called an r-value reference and what this basically is is a reference to an object that someone else is either promised that they're not going to use again or it's a temporary value and which case they buy construction you know that that object is about to die anyway that allows me to steal the contents of that object excuse me and then like basically nuke the object that it came from now I'm not actually you don't see that in this code here it's all in the standard library at this point but I'm accepting it it's me saying like when you call my method ad you are giving me that object that you need pointer is now mine and then because now my function owns it my ad function that owns it when I give it to the in place back method of the expanding array those objects are right there I have to say well I'm done with it now I'm finished with it I have to move it into the caller argument for that the first argument for that function so that's just an example of how ownership is being tracked and transferred now in modern C++ finally you wouldn't be able to go away without saying something about performance and I realize I'm starting to run out of time but I made up something using my funny little pause class that I wrote earlier to just sort of see how good the how well the compiler could do with it and here I'm using like the most functional way I could think of to find the midpoint of a bunch of positions which is to say accumulate them all and add them all up together and then divide them by the number of them that finds like the average of all the points which is if I'm not mistaken in the middle point and of course I couldn't do a talk without having a click on this so this is a an interactive view of that and you can't really see oh you can see it okay so same code and just to sort of give you I mean there's no one's good understand what this is doing here we don't really do but what I would just point out is that that huge block of v mods things here the compiler is able to take advantage of the fact that it knows the memory layout of that array of objects I didn't do anything particularly special you've seen all the code it's up on the slides but the compiler is smart enough to notice that you've got X Y and then the next one is sorry X Y of the first point and the second point in that array will be the next X Y and so on so we wouldn't did you a stream of xyxy in memory and it turns out that this the CPUs have instructions which will just hover up swaths of numbers and be able to add them in parallel to each other using these vector instructions so the fact that it's lined up like this means that the the the compiler can take advantage of the memory layout it can take advantage of the architecture and it can write generate code that is pretty much as fast as you could imagine and I think for other languages where those things are not guaranteed stored contiguous it's very hard for optimizing jits and things to take advantage of that I know there are things coming down the line I think for Java in terms of layout but but and now we're into the but wait there's more of my sales pitch and I'm gonna blast through these because I would like to have time to have some questions at the end compilers there are many compilers to choose from clang has is now like probably the best one to go to nowadays it is just awesomely well developed it has amazing error messages and it supports a whole bunch of tooling which allows you to interrogate and do like refactoring operations on your C++ stuff like Google have really thrown their weight behind this as well so Google are now sort of suggesting that if you're using any of their libraries then you like just pick the latest version of the library and just continuously live at the head version of that and every time they do a breaking API change they're going to supply a tool based on like clang which will convert and move arguments around and do all sorts of clever things on it so it's a kind of an interesting thing that this the clang the compiler has allowed us to do GTCC obviously is a classic and both ICC the intel's compiler and microsoft's compilers are doing a very good job of tracking the most recent changes in the language and GCC and clang nightly bills will have almost all of the things that even being just discussed on the mailing loop lists about what is coming in c++ 20 sanitizers so I've spun you this tale that's totally impossible now to have any kind of memory errors in your program using file pointers obviously that's not quite true there's always a way to muck this stuff up but we have the sanitizers now so if you've any of you have ever used valgrind or heard of valgrind the sanitizers are like a version of valgrind that compiles into your code so the compilers themselves have flags that say hey generate bounced checks for everything hey keep track of which things are live and which things are dead make sure that I can generate an error message at runtime and it slows your program down but at runtime saying hey you did something that was bad and the problem of course well and there are various types of sanitizers we've got memory accesses undefined behavior thread safety there's a whole bunch of things coming but I just like to show you an example of what would happen here so here's my dreadful piece of code which is slightly surprising the compiler actually compiles without any warnings whatsoever but I have an array of ten and I read the eleventh element which is I think actually two off the end of it now if you were to look at the assembly code generated well forgetting the fact that it'll optimize the whole damn thing away probably if you were to look at the assembly it would read off of the end of that array on the stack and that's probably not going to cause it to crash so it's like it's it's broken but it probably won't even crack but what you have got is if you've got this kind of error is a gaping security hole in your program where you're allowing somebody to read on it memory that they weren't supposed to be able to read with the sanitizers on and forgive the shortness of this all but you the thing like three from the bottom here with the big green line here I get this error at the point that it happens the program stops it goes whoa there cowboy you've gone off the end of the the array here you're poking around in the stack somewhere where you shouldn't be you've probably got an error and that's just awesome there's even more output from that down below that tells you all the sort of other bits and pieces that are going on in your program at that time and so if you've got like decent test cases in your code and you should have and you are prepared to build the debug and run all you of your tests with the sanitizer on then you know you're gonna get a very good indication if you've done anything you should ought not to have done even by accident and for what it's worth like although it slows your program down I was able to run a fairly full trading system in debug with this sanitization on and still be able to like run it and not like timeout so it's fast enough on today's stupidly fast computers editors have gotten better there are plugins for everything you know your VI if you're a VI user can have autocomplete the clang tooling is available and is allows like a language server for C++ which is awesome sea lion is is just tremendous if you're used IntelliJ or any of the JetBrains products you now have IntelliJ for C++ which is brilliant the community C++ in the last sort of three or four years I think has really really picked up there's a real the some of the members of the the Standards Committee are putting out like core guidelines clearly stating like okay well we need to support these old things but you shouldn't be using them anymore you should be using these idioms and these ideas and there's a library that goes alongside that that allows you to better adhere to the recommendations that they've given there is a very inclusive C++ community that has a bunch of has a what does it call discord channel there's a slack channel there's some best practices there's a lot of information out there there are tons of things on YouTube if you're interested in like bite size best practices or would like to five-minute videos about like hey here's a new feature of C++ he didn't know it's really picked up recently and so what are you waiting for you should be giving C++ a go right now thank you everyone we've got ten minutes for questions I think yeah I think I write ten minutes and we've got one question in the app but if anybody's got a question now don't be shy read the one from the god it's one of the back there yes hi so the quick the question was if I'm a little bit aware of rust what do I see I'm aware of rust yeah I mean I've written like to toy programs in there and I've looked at the like from my point of view I love performance and things so I like row raytracer and I was like compare you with my C++ raytracer and I'm like oh yeah that seems pretty good actually so Russ brings almost all of the things that seemed lost lost brings in total honesty to you it is slightly more slightly it's much much stricter about the borrowing which I know I've said it's like moving ownership around it's much stricter on that front which allows you to be to be in the situation where if your code compiles you probably aren't gonna fall afoul of anything you don't need a sanitizer as such in Russ because if the compiler come compiler compiled it you're probably okay whereas with C++ it's still possible to play fast and loose which is sort of part of the appeal because sometimes you know your own access patterns and it's very hard to prove too static checker that there the path that it's found by construction of your program can't actually happen so I'm very excited for rust I don't know enough about it to comment more than that though but I think it brings most of the things that C++ has any other questions in the room I was wonder from here so the question was do we use sanitizers in debug bills but not in our production stuff broadly yes although I know that Google are using some of the sanitizers in some of their core components just to for security sort of checking reasons so like cut down things for overflows of like signed and unsigned integers and some of the weird things that compiler can do around those I think they build those into like core components of the Android operating system just to get the protection from it they're like performance is not as important as not crashing and not having a giant security hole so we've got one question via the app which are we - my biggest issue with getting started is understanding the ecosystem how do you start a new project and add dependencies what about deployments and build systems that is yes that is conspicuous by its absence in this talk I'm afraid it the story is not good yet for C++ the best thing in terms of sort of getting yourself up and started with a HelloWorld I would suggest grabbing sea lion Community Edition and just saying file new project and sort of playing around with that that's quite a good way of getting started it builds a seem a project in the background and stuff there's there's a lot of layers of things that are going on there I'm not a fan of that myself but it certainly gets you up and running quickly in terms of adding dependencies there is a couple of current like best-of-breed package management systems for C++ there's something called Conan I haven't used it myself extensively but it worked well enough to like follow the tutorial but yeah with the committee are well aware that this is a problem now and in fact as of the last meeting they have put together a new working group to try and make that onboarding process affecting the tooling around C++ much much better because it's compared to rust I mean that's that's that's where rust shines right I mean I this is a brand new laptop and I I would need to put a whole bunch of things in it I put rust in and I just clicked on the link and typed it through my shell and I had rust running and I just ran one of my projects and downloaded all the dependencies and it built them all and then it ran my application like this this is how it should be for C++ but it's not there yet yeah I'm sorry that's not the answer that the question I wanted I'm certain but what would be a compelling reason to start a new project and C++ I find myself wondering I mean you just heard a big appeal about fighting against entropy throw away the old code yeah with new things that is very true so yeah the entropy that you mentioned - from the the keynote I think that is a real problem but one man's entropy is another person's like core component and there are still some code in systems that I've worked on that have like come from 10 years prior to my arrival and those are probably written in C or early C++ and it's sometimes it's nice to pay that code up and just pump it in and there's no interoperability problems it's just a function like any other function so there are certain things to be said for that that said you know the rough story is pretty good and you know it's interoperability is is I believe pretty good - yeah that's my biggest gripe with C++ all the old ways of shooting yourself in the foot are still they're still valid yeah there's think the very I think Auto pointer has actually been taken out now was it which was the first aborted attempt at doing these smart pointers and it just didn't work and there were some horrific problems were there so but any more questions from the audience tdd I would recommend I use catch the catch to I think it's called and I think it has a BDD and TDD mode of doing it it's a header Oni library it's pretty easy pretty straightforward to use okay oh one there hi so the guideline support sort of the guidelines whatever where they where I'll go back a couple of slides where are we the core guidelines which these are these slides will have links to them and they are pretty good about saying this part don't touch that anymore but they're a huge document now I would say that the vast majority of the standard library is actually fine there's no problems with with it they mean things like Auto pointer which is like the one like glaring problem that has actually been removed or it has a deprecation warning or whatever so I think you're pretty okay with that boost is a high quality piece of software as well there are what else is there I think probably boost is the best one to recommend it as a general-purpose like here are some algorithms and I mean it that's another thing that supercilious is a little bit weak on you like you want to say hey I just want to like change the case of my string I've got my string and I want to upper case you're like ah here's this snippet of code that does it with a stood transform of this to stood up and you're like no no no I won't string to upper case please that's what I'm used to in every other reasonable language ever but these things will be coming down the pike oh I think people are starting to realize that that is a real barrier to adoption for people is it if you can't just dive in and do the stuff you want to do straight away then you're going to very quickly go something else any more questions all right in which case thank you very much Matt thank you very much - cool thank you come and grab there are some compiler Explorer stickers up here if you want to come in grab one
Info
Channel: GOTO Conferences
Views: 49,678
Rating: undefined out of 5
Keywords: GOTO, GOTOcon, GOTO Conference, GOTO (Software Conference), Videos for Developers, Computer Science, GOTOchgo, GOTO Chicago, Matt Godbolt, C++, Programming Languages
Id: HAFrggEDr5U
Channel Id: undefined
Length: 43min 36sec (2616 seconds)
Published: Thu Jun 14 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.