Lambdas in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys my name is Ayana and welcome back to my safe loss class series so today building off of what we talked about last time with function pointers if you haven't seen that video definitely click up there to check it out today we're gonna be talking about talking about Landers and lambdas are essentially a way to a way for us to define something that I like to call in an anonymous function so basically it's a way for us to create a function without actually having to physically create a function just like a quick disposable function if we want to suck if we want to demonstrate some kind of code that needs to be run but we we want to trade it more like a variable and less than an actual formal function that exists as like a symbol in our actual compiled code if that makes sense and obviously when we jump into some examples if that didn't make sense hopefully they'll clear it up okay so first of all a lambda right what is it used for it's one thing to understand what it is but it's obviously a completely different thing to understand how to use it and when to use it and the answer to that is essentially whenever you have a function pointer you can use a lambda in C++ that is how that works right so a lambda is just a way for us to literally define a function without having to define a function so the usage of al and are therefore is wherever we would normally set a function pointer to a function we can set it to a lambda instead right so really to talk about why lambdas are useful and where you can use them you need to understand where you would use a function pointer and we definitely did talk about that in the function pointers video but when we actually start getting into concrete examples in C++ and in this series and especially the game engine series where we actually build something that's when you'll really see me use everything that I described in this series so just keep that in mind this is more to be treated as like a reference and like oh I don't know what this is at all or I don't know the syntax for this actual thing or how it works right so let's jump in and we're going to build off of what we covered in the function pointers video to actually demonstrate the usage of a lambda and the different things that we can do with it okay so this is the code that we had from the last video we basically just built a little for each function which took in a function pointer and in fact last video I even said that we did user lambda this is a lambda right over here we did essentially define our own function in line with the rest of our code that just printed out whatever value we needed and this function pointer essentially defined what the lambda actually needed to be so in other words we know that it returns void and we know that it takes one parameter which is an integer and that's why you see this function as you can see returns nothing it just prints out a line of text and then we have this integer value parameter which is defined because we've said that we need to take in an integer and in this case the integer that we pass into this function ends up being the value that work currently iterating over right so you can see that literally this func value is actually calling this code that we've defined here in our main function and that really is the usage of a lambda lets us do cool things like this one of the biggest examples I can ever give is we want to be able to pass in a function to an API so that at some point in the future it can call that function for us because we don't know we can't call the function now because either it doesn't have the data it needs or we just don't want to we want to kind of defer the calling of a function and if we want to do something like that then of course we need to tell it what function to call when it gets up to the stage if I now want to call a function and lambdas are just a really good way of specifying that function specifying code that you want to run sometime in the future like we have here with is for H where we actually run that code when we're iterating over the element and when it becomes time for us to call it with the specific parameter as we're doing here so let's dissect the syntax of this Lambert a little bit I'm actually going to kind of get rid of this and assign it to an order variable so order lambda we'll say equals our lambda and then we'll pass in the lambda into here all right cool so first of all we start off a lambda with these square brackets let me make some room we start up this lambda with the square brackets what is that now I will point out first of all that you don't really have to take my word for it or this video for it cuz sometimes videos are a bit annoying for things like this you can actually go over to cpp reference calm as I've got open over here and there is an there's a page on lambda expressions you can see they exist in C++ 11 and they've actually got the syntax of all the things you can do and you can see that what of course the first part is the captures and of course it describes everything over here if you've got the explanation you can see the captures are a comma separated list of zero or more captures CPB reference comm is my favorite superclass reference website there are many others and you can use all of them really they're all useful but this one's actually quite clear and refer to this often right I'll leave a link to the lambda page in the description below but in general if you're not sure about something like lambdas or I wonder what goes into that capture thing or what the possible values are right if you try to figure out stuff like that just look it up in the reference ok so inside here you can see the captions we have a comma separated list of zero or more captures so I'll explain what this is in a minute but basically you can see that we can pass in like variables essentially a would be captured by a copy or a value and B is captured by reference here we can pass in this we can pass in just an ampersand which captures everything by reference just an equal sign which captures everything by value or by copy and then that captures nothing so what is this whole capturing thing well consider this as an example what if we want to put outside variables into the instructions that are inside our lambda function what happens then right because remember what we're doing is we're constructing a function that will then get called later so if we use variables that are outside of that lambda like for example outside of this function so maybe I have an aunt a over here that's equal to 5 what if I use that here what if the pop will instead of printing value or something I want to print a well how is that gonna work because this is outside and obviously what I'm doing is calling this code from over here inside this for each loop so how does it have access to a well there's two ways that we can pass this a variable around and this is the exact same as if we hadn't made our own function we can pass it by value or we can pass it by reference and that is what that capture group is for write that first at square brackets that lets us say how we want to pass vary apples in now in this case we're not passing anything in which is why we're getting an error here because well it's an enclosing function we can't pass an A but what we can do is we can either write equals which means pass everything in by value or we can write an ampersand like this which means pass everything in by reference or we can actually write individual variables like this so I want to pass a by value or with an ampersand a by reference now in this case we actually get an error when we try and pass something in well actually if we capture anything whether it be by value or by reference we're going to get an error over here in 4h and this is just in this specific example because we're just using a roll function pointer if we convert this to an STD function like we will right now so this is gonna be of course returning for taking in one hint parameter and we'll call it func I'll include functional so that we have access to that then that's gonna go away okay so what we're doing here is we're passing everything in by value which means it's just gonna copy the value and pass it in but you can also use an ampersand if you want to capture something by reference so maybe if it's like a class or a struct that you don't want to copy or this lambda is actually intended to modify the code if you're not sure or to modify that object or variable you're not sure what I mean by passing by reference check out both my video and functions I think covers a little bit of that but the references video and the pointers video is also related to that all that stuff if we go back to C++ reference we you can also say that we have the next step which is the parameters right those are the parameters that our function takes in we have an optional specifier such as mutable which allows the body to modify parameters captured by copy that's another thing that's kind of useful you can see here that we're copying the a variable but if we try and assign a to something like that it's not gonna let us which is a bit weird because we're just passing by value and of course if we were passing something into a normal function by value of course we can we can reassign it into whatever we want and to fix that you basically just out the word mutable here it's a bit weird but hey stuff like that you probably wouldn't even realize exists if you don't look at things like the reference and see what's possible because you can see that over here we have our kind of parameters and whatever which is one of them is that that beautiful optional kind of sequence of specifies Const expression is another one as well anyway that's essentially how long does work it's pretty simple I don't want to drag this video on because it makes sense I hope to most people it is fairly simple the usages of it are the most important part we're gonna look at one use for why you might want to use a lambda in something called STD find F which is part of the algorithm pedophile and it's basically something that we can use to find a value inside of some kind of iterator so over here we do have this vector of values I've included algorithm over here at the top then I'm going to type in STD find if so this function is basically just going to accept some kind of iterator so we'll give it values begin and values and so between the beginning and end of this vector of values that we have here we can basically say hey returned for me an iterator that is the first element that matches whatever kind of predicate I pass in so in other words what we can do here is write a function is going to take in this is a vector of integers so we'll take an int value and then it has to return a boolean which basically says whether or not it fulfills our condition so we'll say maybe you value is greater than three or something like that let's just return that so return value grid entry so it's going to look through this vector for numbers for integers that are larger than three and return to us an iterator of that which will essentially be just the first element so what it should do hopefully is return five for us so if we assign this order iterator equals a city find F and then we'll print out we'll do reference that iterator and print it up we should get the value five printing why because we've specified that we want a value greater than three using a lambda and five happens to be the first number that is greater than three inside this list of values that we have so if I just kind of put a breakpoint somewhere here and run my code you'll see that we get the value five printing as we expected and we've managed to do that fairly easily by just specifying a nice little quick function that we want this court this code to call so what this is going to do is loop through our list kind of like what I've got here in 4h and it's as if we just added a little if value greater than five and return kind of that value that's basically what this is doing for us but we were able to specify this if statement condition there's bullying by just using a lambda really quickly like that anyway I hope you guys enjoyed this video if you did you can hit the like button that's pretty much all there is to lambdas thank you huge thank you as always to all of my patreon supporters you can head on over to patreon my home 4/2 channel and help support all the videos that I make care of my channel next time I don't even know what we're gonna cover next time to be honest probably we haven't even covered standard function yet but I feel like these function pointers I just gonna drag on for a while and I think you guys all understand them if you don't definitely leave a comment below but anyway I will see you guys next time goodbye [Music]
Info
Channel: The Cherno
Views: 190,206
Rating: 4.9561486 out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, lambda, lambda in c++
Id: mWgmBBz0y8c
Channel Id: undefined
Length: 11min 54sec (714 seconds)
Published: Sun Feb 11 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.