CppCon 2019: Jason Turner “The Best Parts of C++"

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I'm amazed by that one

#include <concepts>
auto func(std::floating_point auto f) { return f * 3; }
auto func(std::integral auto i) { return i + 3; }

The future is best.

👍︎︎ 7 👤︎︎ u/Mordy_the_Mighty 📅︎︎ Oct 07 2019 🗫︎ replies
Captions
morning I think I'm live yes okay cool all right so this is me as Arthur said name is Jason Turner some of the things that I work on also an MVP for Microsoft which he didn't mention though that's fine I do training and contracting this is what I do so if you have never been to one of my talks and those of you is coming in right now you want to come closer because we're gonna have fun in here today this is how we roll there are microphones there there's a good chance you won't get to use them because we're gonna be doing lots of interrupting me and just yelling stuff out and if you're interested in having me to training your organization this is approximately kind of how I do my training as well by the way I'm giving a class on context for immediately after the conference which you still have time to sign up for if you're interested okay back to basics the best parts of C++ I'm gonna cover about 25 different major c++ features this definitely has some overlap with the rnase keynote from a few moments ago because obviously barney is talking about the evolution of C++ as well we're gonna discuss the advantages of each feature mention who was involved in the design as best as I can some of these things names are not conclusive names get repeated because they edit papers that kind of thing partially inspired by Walter Browns lightning talk from VP con 2018 thank you and I'm sorry it took me so long to say it where he thinks all the people who influenced him throughout his career and Shawn parents keynote from Pacific plus plus also on generic programming we talks about the collaboration of everyone involved in the design of the STL standard template library okay who here is stuck on C++ 98 or o3 okay I'm I'm impressed at how quickly you raised your hand just for the record you're like I am II here okay you're stuck in on a pre standard compiler okay well that's unfortunate that's all I can say who is able to you see what's plus eleven Wow things change okay fourteen all right seventeen it's impressive actually okay who is using pre C++ 20 features anyhow okay yeah so like three people raised their hands alright so number one we have a standard this is one of the most important features of C++ internationally agreed to document specifies behaviors of our compilers and programs do not underestimate the value of this not very many languages have this we have competing implementations that try to implement the same standard were able to bet them against each other prove that they do the same things when possible first standard was C++ 98 and many many people been involved in this over the years now I picked up this little programming magazine when I was at C++ on C I think some of you may have in the room may have actually seen this magazine you were there as well it has some really interesting examples that have led to lots of good examples and sites so justly in this example I asked you is it possible for PI to change okay so this program allows PI to change now kanpai as a value does that does that mutable No okay if only there was some way for us to specify that the value of pi cannot change cons all right constable PI tells the compiler that the value not change if we apply const consistency consistently we simplify our variable declarations here just for the record line sub and really really bothers me in this version but if I'm using Const then everything gets better so Const one of those important tools for writing clean code and actually the more I teach than where I speak I think it is actually the most important tool for writing clean C++ najuk declared Const or accessed via constant reference or consular cannot be modified the compiler enforces this East Const or West Const whatever I saw bracelets being handed out of this conference again did you pick up a bracelet alright credit goes to be RNA C++ 98 had this so I started with a single double here I'd like to make a collection of them so I've got my new double of three on the signing values not returning them this code look good what what's that you have to yell louder you said it leaks it leaks okay at leaks I'm caught leak here did I fix it maybe is anyone happy with this code I'm sorry but pre C++ 98 guy maybe you have to be I don't know let's actually let's do this so if only there was some way to automatically delete things when they are no longer needed destructors rai i will get on that okay so we've got destructors and now with my destructor I can delete my data here is your compiler new enough that it has destructors no comment I think it would have to be literally prehistoric to not have this like not even not even seafront I think okay so we can do this I can make my destructor I can delete this array this code has no leak if this code bothers you that's okay we'll come back to it I'll leave it up here for a moment for you to decide if you're bothered about it and why all right now we're on so number three deterministic object lifetime and destruction our constructor destructor pairs are our AII which I heard over here combined with scope values gives us determinism removes the need for things like finally and C++ from the beginning destructor C++ 98 so a lot of these things I say C++ 98 they're actually much older than C++ 98 that's just the first standard I have a handy-dandy tool here that helps me hold some doubles I want one for intz maybe one per floats yeah if only there was some way that I could avoid repeating myself with all this code templates okay templates we declare a class template that can hold anything we want I've got this data thing it can hold things that can delete things for us automatically so I am declaring the class template at the top declaring a function template of halfway down here and it takes three parameters them on to the data member um so just for the record all of these slides are compile and I think this is the first time I'm presenting publicly with this fit down here if anyone really cares you can go to the compiler Explorer link and you can play with the examples yourself so every slide has that automatically generated on it so templates ultimate and they do not repeat yourself principal you can write a template that has types and values that are filled in a compile-time generated probably compiler for you at compile time do not need type erasure now I get the impression based on the hands that you all held up that most of you are not full-time Java developers who are coming to learn about C++ for the first time it's that cool is that correct okay so yeah seamless plus we don't have to do any runtime unboxing of these objects like we have to and other languages with generic this is all compile time stop this is also in the C++ 98 standard for better or for worse templates themselves are turing-complete you can do anything at compile time with templates alright we're back to this code a version of it why does this bother us what's that keep Ella oh yeah keep allocation what if they're not o different sizes of arrays okay okay more than three all right I'm going I think a different route at the moment this code at the moment only accidentally works because of copy illusion compilers have quote always implemented I've gone back to compilers from C++ dies from 1993 and this code would do what we expected to do but without copy illusion things can go bad here we'll keep building on this though so if only there was some way to contain a set of values something that already took care of these issues for us vector containers was that very attic temp no that's it's coming did anyone say list no okay that's fine good vector okay so standard vector of course we can create a vector of these things we can add elements to them and return them now I want to sum up the range of values in here if only there was some way to automatically add up these values accumulate I feel like accumulate is the one algorithm that everyone knows right I have like for each and I have accumulate those are in my tool shed right okay we've got accumulate this is an algorithm it's in the numeric header I can accumulate these range of items so number five algorithms in the standard template library set vector for each any of accumulate generic set of composable tools c++ 98 well not all of them so let's see if these any of was not in c++ 98 yeah I see what's most 11 is when any of was added alexander stepping off mangly and reco Negron is Giustra lots of people involved and the gen and this so this is approximately where my code is now I have a function that returns a vector of three values and I sum them up and return the sum okay this is more generic safer no potential memory issues still in C++ 98 land we know the amount of data compiled time though in these examples if only there was some sort of fixed size container available standard array yeah if you want to like be here Obul you should come closer okay oh yeah you can join me on stage that actually I would be fine for me and would keep the camera crews jobs much more exciting actually so we know the data compiled time so we've got a fixed size container available standard array no dynamic allocation win-win scenario with knowing the size of the data structure compiled time so Lenny Mayer Ani is he in here you're actually near Lenny awesome you know what I try to quote you when I break in on this one because Lenny coined this for me as a negative cost abstraction we like to talk about zero cost abstractions in C++ when we give the compiler the size of the data at compile time it can do all kinds of crazy things right lady alright very good did I pronounce your last name correctly close okay going close that's good alright just for an aside when he goes to my local meetup who goes to your local C++ users group come on raise your hands if you go to your user group are you kidding me how many of you live in Denver and you're not coming to my users group really okay we meet on the first Thursday of the month and now the entire world will know that okay shared right big sized stack based container having the size part of the type information gives us more optimization opportunities so that I didn't see what's supposed to love in as far as I tell based on boost array which as far as I can tell was written by an eco so I would like a version of this that takes two parameters like that and a version that takes one parameter well I didn't ask if there's any way to make this better yet I'm still waiting for someone to comment on the code up here yeah why does that first one have to that is what happens when you copy and paste code all right thank you so it fixed that now Poli there isn't a way to initialize the reason one step someone's been trying to say berry attic templates for like 10 minutes over here but we're not there yet okay we want to initialize these array values in one step what are we gonna use initializer list list initialization here that is really handy simplify this code and it can become this because we can do this bracelet Ischl ization on the return to initialize the standard array starting to drastically reduce this code list initialization C++ 11 it has its issues in some places we're gonna focus on where it really helps our code at the moment if you really care I did an entire hour and a half talk called initializer list are broken but we're not talking about that right now see less plus 11 gabi dos Reis and bareness root stroke involve cure so I have this version I want to add a four five six parameter version I add the fourth one I add the etc now if only there was a way to avoid all of this code duplication very attic template's okay alright very otic templates so I've got my standard array the value the size of the params now I'm in this particular code deducing the type of the array based on the type of the first parameter sent to the parameter pack that is what I have chosen to do in this specific example so that is the very first parameter is VT I'm using that here and then I have to do the size of the remaining parameters plus one to get the actual size of the array that I want to return get something like this muriatic templates drastic simplification of code needing to match a variable number of parameters this is critical for maintainable implementations of things like standard function so anyone ever tried to implement something like standard function pre C++ 11 oh my goodness right did you use boost VP or did you do unroll it all by hand you what oh you generated the code that's pretty much sunnis using boost BP really just are you asking the preprocessor to generate the code for you okay so going back to this original example is PI known at compile time I mean like arguably it like compile time of the universe basically right okay if only there was some way to make a compile time constant here gods Dex per context for double PI here we go I can either use a context for function to generate PI with something like this there's something slightly more clever because well let's be fair that's not a very good approximation of pi is it can't stack spur compile time generation of code and data see what's less 11 majorly relax the restrictions in cos plus 14 I need to update this slide I just realized because the restrictions for C++ 20 we're pretty much no-holds-barred at this point we can do very many things in context for time now what is the type of Pi which should be float double long double integer short character I don't care it depends who says I don't care he says it depends keep your hand up if you do scientific computing Oh interesting okay I just expected a slightly larger group of people to do scientific computing all right I generally fall into the either I don't care it depends so if only there was some way to indicate that I don't care what the type is yes variable templates not at the moment though not today auto auto is what I was looking for here Auto see what's what's the love then Yakko I guess I'm terrible at some names bjorna Gabby again involved here this is one of my favorite quotes this is from B RNA C++ 11 fact website I love this particular quote I will read it the auto feature has the distinction to be the earliest to be suggested and implemented I had it working and my C front implementation in early 1984 but was we forced to take it out because of C compatibility problems these problems disappear when cos plus 98 and c 99 accepted the removal of implicit int well I'm going to come back to that in just a moment that is both languages require every variable and function to be or defined with an explicit type the old meaning of auto this is a local variable is now legal several committee members scroll through millions of lines of code finding only a handful of uses and most of those were in test Suites or appear to to be bugs I will demonstrate the bottom so in the the top one here and C++ 98 auto is redundant I am saying this is an automatically managed lifetime variable Auto that's what Auto inta means in this particular case in C++ 98 and the bottom case pre C 99 this is an automatically managed integer because inch is the default type this is a valid program in pre C 99 I don't have to specify the return type main because it is implicitly int I don't have to specify the type of I because it is implicitly int if I were to do Auto I equals 5.5 and pre c 99 what type is i it's an int right it's just an int it's just oh hey I'm declaring a variable don't worry about it it's default in everything's default in now is there any way to adjust that previous example by the way if I can get to it to say I really really don't care what the type is here we've got Auto what's that whoops deckle type now we don't want deca type what do you say double am i they're gonna come down there you're gonna speak up okay change double to auto thank you that's what's going for full auto return type deduction that's what you're aiming for all right cool or something more clever still anyway yes so I did that okay I like auto return type deduction because auto return type deduction for normal functions added in C++ 14 I'm gonna have to move slightly faster I just realized but it's good this allows for things like this this is possible in C++ 14 was not possible at all in C++ 11 now you might look at this and say why the heck would I make my own type inside of this function and return something that is opaque here there are uses for this we'll build on it in just a moment this allows for the creation of powerful higher-order function constructs which by the way I just did a C++ weekly episode last week on higher-order functions so you can watch that so I have this example I have a map that I'd like to print I'd like to print a set of key and value pier pairs from a map and C++ 98 or O 3 we know that auto could help but we're in C++ so 3 we can't use it best practices Shawn parent famously you know tells us what here now we're all loops thank you so let's use an algorithm I'm using an algorithm C++ O 3 what does that look like what do I need an object that okay that is what was that a functor you know you know what I'm not going to get into some sort of pure math argument with you so let's say function object just for the sake of YouTube so we need a function object so an object that's callable and C++ oh three callable like that we also need a way to capture our key description and value description because we have to forward those to the callable thing that we're passing before each so this is an implementation of printing this map and c++ 98 i've got my map value printer that takes these key and values and captures those into a local variable here i've got my call operator with its constant value thing it's a templated function I can print whatever I want to here using oh stream and now I can say for each from the beginning to the end of the map print the values using these keys and descriptions keys and value descriptions that was fun right now all of you who raised your hand saying I'm still in C++ 98 or o3 raise your hand back up if you make good use of algorithms all right now let's say I am now in C++ 11 if those of you who are or better do you make good use of algorithms okay if only there was some way of creating a handy callable thing for us using an algorithm what do we use lambdas so I can use this lambda this is the C++ 11 lambda I have to here say I want the proper type of the contained value type of the map lots of type names and colons and such lambdas lambdas laws to create unnamed function objects which may or may not have captures we are not allowed to know the name of the type of lambda we are not allowed to call its name save us plus 11 for the sake of expediency I'm going to stop trying to pronounce the names on the bottom of the slides that they are up there this is a bit wordy so how do we make this better Otto I love Otto is Otto your best friend and C++ okay so I can use Otto here this makes a generic lambda and number thirteen generic and berry attic lambdas create implicit templates for us by using the auto keyword here added in C++ fourteen that's the general structure of a lambda I'm gonna go back to this pre algorithm one for just a moment this is our for loop with our type name consider eight or templated function I'm going to factor out the template I'm going to look at this now I know some of you still said you're in C++ 98 so most of you are not but I just want to say for the record when I was personally learning C++ let's see if I can get my wand to work here this bit how to do a for loop over a container the map int string consider a tour hitter begin equals Donna's not equal to the end increment the iterator this thing right here was like one of the most difficult things for me to learn programming in C++ before I understood what I was doing I felt like I was casting a spell here or something right if I apply Auto I get to do this that's C++ 11 if only there was some simple way to iterate over all the values in a container range based for loops I've got a pretty good group right here that's yelling all the things out except for the occasional straggler over there right side of the room you need to pick up your game okay I thought I'm just not hearing you range bass for loops iterates over all the elements in a container works with anything that has begin and end members or functions available for them so these also work with C style raise an initializer list if you did not know that this was added in C++ 11 Doug Gregor Jionni's true step involved in this now this I'm gonna do something here it's only a second time I've tried this Oh fail nevermind forgot to launch an extra application here okay this bit right here this data dot first and data dot second seems a bit like I mean what am i what am i doing why do I have to do this like if only there was some way to make this more readable what would I use what's up structured bindings good job front row I'm gonna have to throw you a prize or something but you have to then tear it amongst yourselves and each get a piece alright structured bindings so we can do this here in C++ 17 now my favorite use of structured bindings is like this because it makes our body of our loop more readable I can just say key and value down here used to decompose a structure or array into a set of identifiers you must use Auto and the number of elements must match at the moment at least there is no way to skip an element I know it as a frustration for some people this was added in C++ 17 all this whole template syntax that I keep showing here this is a bit bulky only there was some way to simplify this template syntax anyone ooh what's that template arias no it's not well it's not what I'm looking for but that might be an acceptable answer what's that Oh concepts all right sorry accidentally skip the side I have the wrong act what I have is I have the wrong line highlighted so it threw me off right here Const Auto map right here see boss plus twenties Auto concept this is in the terse concept syntax which I believe has been officially approved but no compiler I believe implements it at the moment although this particular code will compile with GCC because it has older concepts support way it's a thing anyhow so we can do this this is implicitly created a template for us just like with our lamb does take this code I want to have two versions of a function one takes the floating point one takes an integral value if I want to do this in C++ 11 using type traits this is what it looks like who like loves spin a who knows what spin eight is okay this is fine who has actually had to write spin aid functions okay so a lot of you have done it but only one of you actually liked writing this code okay it's a ND concept it's not very friendly by the time we get to c++ 17 this code it looks like this by the way i do these underscore b's and underscore t's and such with concepts we get to this now if i'm reading correctly these floating-point and integral concepts have been officially approved for c++ 20 all right I got a nod in the back so I can say floating point Auto F integral auto I and it will call the floating point function if I pass in a floating point otherwise it will call the integral version let's be fair right if you write a function that when it takes a floating point multiplies the value by 3 and it takes an integral and adds 3 to it don't do that ok this is slide where concept definitions themselves can actually be quite simple we can just build them up actually from our type traits but the standard is doing that for us already in C++ 20 new keyword new syntax too used to allow us to specify the requirements for a type implicitly creating a template that constrains how a function can be used coming in C++ 20 concepts has a very very long history in C++ the original discussion of it started some decades ago and the but we have it now proved in C++ 20 originally originated by stepping off here this code that I keep coming back around to has a potential inefficiency hiding in it Kian value are by no there by reference everywhere here okay that's reference that's did I misunderstand your comment okay what's that but you said copying strings I'm actually looking slightly one step more fundamental than that allocation of of strings like I don't even need the strings here at all so I'm constructing a string from a Const car star for really no particular reason so if any there is some way to observe a string like thing without actually having to construct a string string you alright so I can use string view here these are by value on purpose string views are cheap to copy it's generally recommended to pacify value I could have done them by constant value here they are passed by value this code now no longer creates a string at all if not Cerie for these values and keys descriptions string view not owning view of a string like thing by the way I have abused string view I have put bytes in them I've done things with them and you can do whatever you want to as long as the contiguous data structure Andrew Sutton Casey Carter Eric nee blur I think I've been involved in this from what I can tell based on the papers involved in the string view proposals see out quite verbose relatively slow difficult to reason about if only there was some easier way to format our output Britten for those of you who did not move closer we got the peanut gallery saying print F up here that is not what I am looking for I did hear what FM t okay format lib I can do something like this now I am not yet sure if this is a 100% good idea but the formatting library as it has been accepted for C++ 20 does not have any way to print but it is generating a null-terminated string puts is an extremely efficient way of writing out an all terminated string with an implicit carriage return at the end of it which is what I want to do in this particular code so I am using standard format see bus plus 22 format up this string use C string to get a null terminated string passed that to put I'm still deciding that's a good idea yeah rearrange for translations just a second okay my clicker stopped working okay there we go text formatting subset of the excellent format library is being worked on for soups lows 20 I should've updated to say has been accepted for zelos 20 I still think there's some few details to iron out allows for formatting of our strings with positional named and Python printf style formatting options so that is indirectly the answer to your question I can do something like this pass these in an opposite order so I can have different regional strings that have things in different orders and request different formattings for them and yes it should make doing that kind of thing easier Victor this victor in the room is Victor its EVP con yes I need to say hi okay bringing together algorithms text formatting concepts etc our map printing routine might look something like this in C++ 20 I've got a Const auto map that is a auto concept this is an implicit template string views I've got lambdas I've got algorithms and I'm reaching a point that I really like when I'm saying I'm pretty close to refactoring my code personally because I can come down here and read this as a sentence theoretically for each from the beginning to the end print the key value I like that I like it a lot but only there was some way to avoid having to do this begin and end nonsense every time we call one of our algorithms ranges all right I got a big answer from that one all right so ranges for each map print the key value now this is a part I am still unclear on but I think I should be able to do some sort of piping operation here with ranges although nothing implements it currently as far as I can tell ranges coming in see west los 20 this is the example from CBP reference comm I think it is correct but you can do things like take a set of integers filter out the even ones transform them with the squares and print them all out although I should have I should have what here instead of see out yeah what Essen would it actually make sense to put format right here since form I can't print I don't know alright we'll have to talk about that later or something all right Eric Kneedler has done the bulk of the work on that Casey Carter as well [Music] get back to our data creator I require at least one parameter and it sets the type here alright if I apply Auto return type I get this did that did that actually help anything auto return type here well I don't think so I still have to do the exact same thing I just moved it a line down basically only there was some way to automatically deduce the type of the array being returned see tab class template the class type argument the test template argument adduction way I can use seat add here and see bus plus seventeen standard array BAM types it automatically our objects automatically figures out the types for me and the size of the array this code gets much more reduced just how function arguments have always been reduced class templates can be reduced as of c++ seventeen four types like array this has a giant impact for types like vector it's kind of interesting we can do this this is a vector it's going to dynamically allocate space for three integers that's just what it's going to do I can simplify my template arguments to this now I did have you know the first one broken out but now if I simplify like this now and to be perfectly fair this might you make using this function a little bit harder as its going to result in a compile-time error if any of these types differ from each other I don't have any way of constraining those at the moment to all be the same type of thing see the celeste 20s auto concept we can remove the template and we can come down to this only problem now is that this code requires the types to be copyable if only there was some way to Ford arguments and avoid copies forwarding references like this no unnecessary copies of parameters guaranteed copy and move illusion of the return value we'll come back to that sentence that's a little early okay admittedly though I must say because I teach this regularly this does not make c++ easier to teach okay understanding exactly what porting references are how they're deduced calling standard forward I mean this is what this looks like and doing this deco type here because I'm using auto if I had had the template syntax whatever but what we reached now is this sentence right here which I think is very important at this point we question if get data is even necessary at all right I just added complexity back into it but wait a minute if I go back to this version and we focus on line 5 it's a standard array of some objects I don't need a function I don't have to worry about forwarding or not we can get code this simple depending on what we need to do our value references that was the point of that one can be difficult to each undeniably fix a few holes in language that allow us to more accurately reason about the lifetime of objects Howard in it Douglas Prager David Abraham's see what's plus 11 I said guaranteed copy illusion all right compilers have quotes always done copulation on return values and C++ 17 it is guaranteed in some situations note this code this is the move constructor on line 2 3 I have deleted it on line 4 I am deleted the copy constructor this object should not be copyable or removable I am returning it from a function this code compiles in C++ 17 it is required to compile on C was plus 17 this is neither a copy nor a move I love this this is like well it's the standard says it's two different ways of referring to the same object when this comes into play but it's cool stuff credit goes to richard smith on this why nice codification of the optimizations from the earliest things that compilers have done yes if it's not a copy of move what is it doing how much time do we have [Music] now I can click on any of my slides and we can edit them I didn't do that did I before all right only problem is knowing where my home keys are all right so if I do this when I misspelled Bell you know you okay it's not a copy it's not a move what is it that's what's the question okay and I think I have time to do this so we're gonna do it I need a volunteer come up here quick someone I'm serious yeah never done this in a conference talk all right Yan Yan you are value you're value Yan was created right here he is value a pointer to Yan was passed forward into S factory where the object was initialized Yan never moved he was always right here it is not a copy or a move it's two different ways of referring to the same object on line 8 over here I'm still referring to Yan Yan never moved that's the simplest way of looking at it we can play with this and one of the later toxin this week I actually go in a little bit more into detail on on how this works and passing the memory location of return value from a function comes into play thank you yawn okay all right what's that how can what Oh a place placement new placement new is still going to have its uses in the places that truly need it reusing the memory location for a new object is where it's going to come into play or constructing objects in a blob of data that was heap allocated for something like vector or something at you won't be able to completely avoid it all right okay so I promised that I'd come back to this one again and I asked I mentioned that this relies on copy elision how exactly is this broken though does anyone know it doesn't move it actually does move Owen Owen C+ was 98 okay yeah sorry and see once 98 it's a copy and see what's less 11 it's a move potential double delete okay so if I am copying this thing because it seemed lost most 98 even though all compilers had copy illusion I'm doing a copy which means I've got two different copies of the same pointer theoretically hypothetically two different destructors that are going to be called although it never actually happened that way in code this simple so we moved to see what's plus 11 it's now a move and a the built in move of a scalar type a pointer is implicitly a copy C++ 11 doesn't change anything here we still get the double free default move constructor will copy the pointer so I've already hinted at this but if only there is some way to disable problematic operations delete we can just delete all of these things this thing I say not very useful at the moment that's not very fair though because we know and C++ 17 we can still construct one we turn it from a function its constructor and destructor will be called properly everything will be cleaned up well you know it is safe no way to accidentally copy delete a saw or copy or move or assign to this thing so defaulted and deleted functions any special member function can be explicitly defaulted any function at all can be explicitly deleted this makes our API is harder to use wrong this is a very powerful feature most people don't appreciate that you can delete any function if you find that the users of your library are calling you know your function that only expects the double with like integers all the time or something it's a band-aid of a fix at the moment but you can just delete the integer overload and break their code in a good way because we don't want them using our API is wrong lawrence krauss worked on this one so only there was some way to not even have to think about these seep allocated memory other than the containers that we've already mentioned what might we use smart pointers okay we can use unique pointer here beatnik pointers safe with arrays standard unique pointer standard make unique number 24 these are automatic safe very difficult to use incorrectly objects interestingly we are reaching a point where this is all starting to build on itself they rely on deleted special member functions they rely on destructors they rely on r-value references they play very nicely with our copy and move elision for our factory functions they came about C++ 11 I have to say slash 14 why do I have to say slash 14 make unique there's a little an oversight we had make shared in C++ 11 but not make unique we got that in C++ 14 apparently was originally called move pointer implemented by how her hen it when he was working on the r-value references okay I'm gonna make a quick summation function with my very addicts here how would I sum the values pass to this function I have so many options I could do it like this recursive periodic expansion oh no well yeah but like I'm in C++ 11 I have my very odd octuplets for the first time what else do I do actually what else do I do if I'm okay why Oh No well besides I say it's horribly slow and difficult to debug I guess those are good enough reasons nevermind ah okay does anyone actually know a different option and see what's plus 11 what's accumulates I can't do accumulate well stick it in an initializer list and accumulate the results I can't do that because I need to call begin and end on that initializer list I mean I case if I might know local initializer list and then did they that work does that say so work within the lifetime rules that might work okay I'm gonna go with this like this that's not what you were actually saying you were calling say standard accumulate on abusing the initializer list as far as I know Shawn pair and Eric Kneedler first published this there's a Twitter thread that you can find that it's fun and interesting and I am really pacing a lot apologies over here I have used this trick by the way it avoids the recursive expansion of our very attic template functions it is actually considerably faster to compile and I wouldn't say it's readable but I get the first value out here using this auto and then I am doing a summation here I'm adding the next value to the current result what is this comma do what's that yes all right yes so it well it returns the right side it returns the last most one so I'm performing this operation regardless of what its type is and what type it returns zero is what is actually going to be used to initialize the initializer list and then I'm casting the initializer list avoid to avoid an unused variable error or some other nonsense here and then returning the result here this works by the way something like this all right so if only there was some way to ask the compiler to some of these parameters for us huh bold expressions yes so I can do this hold expression C++ 17 looks like that that's pretty neat fold expressions are producing very attic expansions can be used with any common operator stars enter credit goes to Andrew Sutton on this any common yeah it's it's weird the stuff that you can do with old expressions so our key features C++ 98 we have a standard Const deterministic object lifetime templates algorithms in the standard template library C++ 11 standard array list initialization very attic templates concepts for auto lambdas ranged for loops the our value references default and deleted functions unique pointer sealless less 14 relaxed context where this is pretty big generic and very attic lambdas return type deduction for normal functions and stood make unique it's its own special mention seamless plus 17 structured bindings string view class template argument deduction guaranteed copy illusion fold expressions symbol s 20 concepts text formatting ranges that's everything that I covered here there's of course many many other things here there's a bonus comment here that none of you comment just for the record but what is the return value of pi' here it's 3.0 yes it that I have double in this particular version I'm not using Auto return I'm doing integral division I expected someone to yell out something but you did so I'm doing integral division here this is three that's yeah three and three dot that's technically correct that's a double how do we catch something like this Curly's no that's true but I'm looking for something slightly different I only have three minutes left compiler warnings tools we are in a heyday of C++ tools you do not even like if you are if you've been programming in C++ for less than 10 years you cannot possibly imagine what we used to have to deal with okay we've got sanitizers compilers with amazing warnings today static analysis testing frameworks fuzzers we even have tools that can automatically modernize our code for us if you're not using this today whenever you're allowed to leave your pre standard compiler run clang modernize on it so what do I see common idioms get easier to express language semantics get more consistent we've got very attics and more places auto in more places memory management issues should basically completely go away in our modern code we don't have to think about the lifetime of our objects Yann thank you for being a good visualization Yann always lived here right okay it's easier to construct libraries that are hard to use wrong do not underestimate this this is critical for writing good code we haven't hit on the power who I actually need to remove that having done the power of contracts contracts were removed from C++ 20 but we can properly constrain our functions arguments with concepts these are some of the names that have been involved over the years many other names they couldn't find and well we have a minute left this is me I will leave that there if anyone wants to ask a question you can otherwise yeah I do you want to use the mic though if you're actually going on because you're standing right next to it a lot I did you know don't you think that it is just a batch for how ugly types in C++ can be and if you actually have the type in there it's clear for the person who's reading the code and most of the places where I personally use Auto is code where the type is unknowable so it's irrelevant because I'm doing generic programming or some sort a lambda something like that so I don't I have no choice but to use Auto I think in many cases Auto is underappreciated because if we use Auto the possibility of invoking an implicit conversion is effectively zero implicit conversions can cost us a lot depending on our code and both readability maintainability and actual runtime performance I did not use to be a big fan of Auto but the more I had used it I'm not just for the record I am NOT the person standing up here saying almost always Auto right I'm not saying use Auto everywhere but I'm saying appreciate how I can avoid implicit conversions and how it can give you more expressiveness with the things that you're doing thank you sure anything else all right thank you everyone [Applause]
Info
Channel: CppCon
Views: 65,078
Rating: undefined out of 5
Keywords: Jason Turner, CppCon 2019, Computer Science (Field), + C (Programming Language), Bash Films, conference video recording services, conference recording services, nationwide conference recording services, conference videography services, conference video recording, conference filming services, conference services, conference recording, event videographers, capture presentation slides, record presentation slides, event video recording, video services
Id: iz5Qx18H6lg
Channel Id: undefined
Length: 58min 36sec (3516 seconds)
Published: Sat Oct 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.