Lambdas in C++

  Переглядів 326,697

The Cherno

The Cherno

День тому

Patreon ► / thecherno
Twitter ► / thecherno
Instagram ► / thecherno
Discord ► thecherno.com/discord
Series Playlist ► thecherno.com/cpp
Lambda Reference ► en.cppreference.com/w/cpp/lang...
Thank you to the following Patreon supporters:
- Dominic Pace
- Kevin Gregory Agwaze
- Sébastien Bervoets
- Tobias Humig
- Peter Siegmund
- Kerem Demirer
Gear I use:
-----------------
BEST laptop for programming! ► geni.us/pakTES
My FAVOURITE keyboard for programming! ► geni.us/zNhB
FAVOURITE monitors for programming! ► geni.us/Ig6KBq
MAIN Camera ► geni.us/t6xyDRO
MAIN Lens ► geni.us/xGoDWT
Second Camera ► geni.us/CYUQ
Microphone ► geni.us/wqO6g7K

КОМЕНТАРІ: 230
@satyamraj2779
@satyamraj2779 Місяць тому
I don't know why, but that sofa and the tree swaying in the backyard , bright sunny day, it just all feels awesome and nostalgic..
@dsedchenko
@dsedchenko 5 років тому
The best cpp video tutorials I've ever seen, thank you!
@ToxicityGameDev
@ToxicityGameDev 6 років тому
So glad you are still doing this mane! Keep up the good work! Document everything you know about C++ its so great to have this playlist at my disposal :-) Thanks as always!
@jinyoungpark3277
@jinyoungpark3277 5 років тому
THANK YOU SO MUCH for this video!! You made everything clear
@fishyperil2153
@fishyperil2153 6 років тому
The thing that used to confuse me the most about lamdas was that I didn't understand why there was such a thing as a capture bracket for passing arguments when lambdas also provide the standard way of passing arguments just like ordinary functions do. Then I read somewhere that by using the capture bracket, the variables in the surrounding scope get treated as if they were member variables of the object created by using the lambda. I think this ties in to example you made in the video - when you tried to pass the lambda as an argument to a function that accepted a function pointer as a parameter , it only worked as long as capture bracket wasn't being used. It only passed the functionality defined by the lambda. However, if you want to pass the entire object that gets created by the lambda, together with the variables captured in the brackets, you need to pass it as a function object instead, not just a function pointer. So, in conclusion - arguments passed as parameters get treated just like they would if you were passing them to any function. Variables captured by the brackets become a part of the lambda object.
@obabamaiu
@obabamaiu 6 років тому
Thanks a lot for this. That part of the video left me confused so I came to the comments looking for some explanation... Yours made everything clear! Cheers.
@rj00a
@rj00a 6 років тому
This code helped me understand lambdas when I was first learning about them. std::string str{ "this is a string!" }; auto lambda = [&](int num) { std::cout
@goku14139268520
@goku14139268520 5 років тому
This thread has been incredibly helpful. Thank you to both of you guys that offered an explanation.
@groberti
@groberti 5 років тому
This is a basic and implicit functionality of lambdas in other languages (ie. C#, JavaScript) but here in C++ you have to explicitly state that you want the given variables to be part of the lambdas scope. It is a weird choice imho and I have no idea why didn't they just made it so that these variables are implicitly already part of the lambda's scope just like in the languages the I mentioned before.
@sachinnale1076
@sachinnale1076 5 років тому
Well as you described very well, just adding some more light to it it looks like this. int main() { int a; int b; auto lamb = [a,&b](int value){ b = value; return a+b; }; lamb(10); return 0; } it looks like this. int main() { int a; int b; class __lambda_7_17 // random name generated for lambda class { int a; int & b; public: inline /*constexpr */ int operator()(int value) const { b = value; return a + b; } public: __lambda_7_17(int _a, int & _b) : a{_a} , b{_b} {} }; __lambda_7_17 lamb = __lambda_7_17{a, b}; lamb.operator()(10); return 0; }
@Kazetomosuki
@Kazetomosuki 3 роки тому
Wow, this video in combination with the previous one about function pointers was an enlightenment to me! 😄🙏👏 Thank you!
@chetannaik5199
@chetannaik5199 2 роки тому
I can’t thank you more. This had puzzled me for more than 6 months. I just felt so relieved and rejuvenated today. Thanks Cherno.
@dimasveliz6745
@dimasveliz6745 3 роки тому
Thanks for the clarity of video! Especially for the second part that includes the functional thing!
@vegansynths7757
@vegansynths7757 3 роки тому
This is super awesome. And thank you for inadvertently showing me how to put in a breakpoint in my IDE. Never knew I could do that. :)
@GeekyMino
@GeekyMino 6 років тому
Dude your channel is awesome! Your videos are super informative and useful. Keep up the good work :)
@svddwd
@svddwd 5 років тому
Thank you so much.. I was not clear abot Lambas ever after reading so much.. You made it look so simple.
@jinshiro
@jinshiro 3 роки тому
Thanks for this, the learncpp section on this nearly cooked my headmeat.
@jianiliao7140
@jianiliao7140 6 років тому
Thank you for making the video!
@samitkapoor53
@samitkapoor53 3 роки тому
Thank you, this was so helpful!!
@YoutubeFrogOfHell
@YoutubeFrogOfHell 4 роки тому
7:00 "Sure thing now it's not gonna work. we need to convert it and add blah blah one billion obvious other things... aand... here we go!" .___. how you do this man
@rcookie5128
@rcookie5128 6 років тому
finally clearing up a topic I tried to avoid as it confused and overwhelmed me when I first saw it.. thanks!
@sakshigupta7616
@sakshigupta7616 4 роки тому
Hey, I didn't understand that what was the use of ForEach function in this video it would be great if you could help me :)
@TravelingMooseMedia
@TravelingMooseMedia 4 роки тому
Sakshi Gupta bruh
@susiana5
@susiana5 6 років тому
Thanks for uploading :D nice!
@Rahulsingh-theraha
@Rahulsingh-theraha 7 місяців тому
honestly a great series for programmer who have past exp , did whole thing (till lambdas)in like 3-4 hrs and understood almost everything .
@joushuahung4996
@joushuahung4996 3 роки тому
Top quality tutorial, Thank you.
@oatmealduck7937
@oatmealduck7937 4 роки тому
These are so fun to watch man
@christianm4906
@christianm4906 5 років тому
Man, you saved my day.
@shat104
@shat104 5 років тому
Damn this guy is so good, must feel pretty good knowing that if you interviewed anywhere you can get the job.
@phagesuffersatgaming.3797
@phagesuffersatgaming.3797 3 роки тому
@Lance Adan this is a scam, don't touch, report those comments as spam instead.
@puppergump4117
@puppergump4117 2 роки тому
@Over Yonder What kind of idiot would put their business at risk just to screw over their workers?
@nakon7niket
@nakon7niket Рік тому
@@puppergump4117 I think what they meant to say is that having a micro celebrity of C++ space as an employee would make the company walk on eggshell and give him special treatment because he can use his internet clout to destroy the company's reputation. Nobody wants to screw over their workers but sometimes harsh decisions are made and some people dont take to them lightly but those people also doesn't have a decent following so they are not a risk. I mean just imagine having someone like Joshua Fluke as your employee.
@sankar9483
@sankar9483 5 років тому
Thanks lot. Wonderful Video.
@shadyganem5448
@shadyganem5448 3 роки тому
great video very helpful C++ is a powerful programming language but it has so much syntax. for example there are 4 ways to define a lambda function. this is just confusing and makes code unreadable I'm starting to understand why old school programmers prefer plane C. you have structs functions and pointers. with that they can abstract anything they want.
@kelvin31272
@kelvin31272 8 місяців тому
Thank you Cherno! Finally understand it
@austinbryan6759
@austinbryan6759 5 років тому
2:24 This is almost the exact case I was wanting to use this for, funny enough
@shanielsamadhan979
@shanielsamadhan979 6 років тому
Start the game engine series soooon please. I'm excited af!
@mryup6100
@mryup6100 2 роки тому
This content ► Amazing explanations
@mangrubengaming4300
@mangrubengaming4300 4 роки тому
You really know what you're doing. You're so good. Do you have a video about ECS?
@Kapil_Thakar
@Kapil_Thakar Рік тому
u are my favorite, Amazing, u kept it very clear...
@boody7665
@boody7665 5 років тому
awesome , Thank you
@asajjad4542
@asajjad4542 3 роки тому
You're amazing!
@ahmedanwar976
@ahmedanwar976 5 років тому
Thanks man
@achrafbahloul192
@achrafbahloul192 2 роки тому
so helpful thanks
@tomdapchai
@tomdapchai 7 місяців тому
thank you
@madhvishrivastava3646
@madhvishrivastava3646 Місяць тому
Thank you for explaining that complex topic in a very simple way. A big thanks from India.😊
@alexsece
@alexsece 5 років тому
Thank you!
@EezySeven
@EezySeven 6 років тому
You should make a video on C++ iterators, dude.
@gvcallen
@gvcallen 4 роки тому
please!
@greatbullet7372
@greatbullet7372 3 роки тому
@@gvcallen he did today ;)
@mrmrigank7154
@mrmrigank7154 2 місяці тому
best cpp and one of best programming channel ever
@aurinator
@aurinator Рік тому
Would be interesting if you highlighted more about why they're preferred to function pointers. They've become almost a standard in many different languages.
@UsernameUsername0000
@UsernameUsername0000 9 місяців тому
I think the previous video did that. Function pointer syntax is daunting and you’d have to define your functions at a larger scope than ideal to use them.
@Inglonias
@Inglonias 4 роки тому
I'm reminded of the noun-verb system used on the apollo guidance computer. A lambda variable would sort of be an adverb, then? The best example for when and how to use this that I saw was an example quicksort that took, as a parameter, a comparison function. This allowed the same sort function to be used for any type of data you could think of, so long as you could code a function that told the quicksort which data element was larger, which is slick as hell.
@DUFLEBIN
@DUFLEBIN 6 років тому
Hey great playlist on c++. Can you make a video on iterators in c++?
@vertigo6982
@vertigo6982 5 років тому
One can never have too many function pointer, functor, and lambda videos.
@LeSaboteur3981
@LeSaboteur3981 3 роки тому
great, helps a lot!
@DawitBeza-nr1tn
@DawitBeza-nr1tn Місяць тому
you know the way you teach me is exactly the way i want to be tought
@ashutoshaswal
@ashutoshaswal 3 роки тому
THANKS
@abhishekchavhan3768
@abhishekchavhan3768 Рік тому
Amazing
@mba2ceo
@mba2ceo 6 місяців тому
What makes him a great teacher. 1. Confuses the HELL 2. then explains everything 3. GOD DAMN MY PROFESSORS
@AbhishekKumar1902
@AbhishekKumar1902 5 років тому
The best tutorial on the internet!
@Muhammad_Abdullah_Sultan
@Muhammad_Abdullah_Sultan Рік тому
Thanks
@clementboutaric3952
@clementboutaric3952 5 років тому
The level of programming you have to possess to encounter situation in which such mechanics must be used is unfathomable.
@nutritionalyeast7978
@nutritionalyeast7978 3 роки тому
actually i just encountered these in the wild, in an example for a library. and im by no means a highly experienced programmer. It was used in a specialised "for each" function just like in this video
@Codeaholic1
@Codeaholic1 5 років тому
Great video. The couch made me laugh.
@murhafal-msri5479
@murhafal-msri5479 Рік тому
I would to point out that this subject was very hard for me before using map () in javascript. Basically it is that same thing. And that is way learning new language may help you understand the other
@priyadarshini.d4980
@priyadarshini.d4980 2 роки тому
hey.. thanks for all these videos on C++.. Could u also make a video on Functors in C++?
@Gojam12
@Gojam12 2 роки тому
Your the sh** mr cherno, rock on brother
@joeporsche1767
@joeporsche1767 3 роки тому
You are the best for c++.
@arod3295
@arod3295 2 роки тому
bless yo intelligent, puzzle solving soul
@pavliv
@pavliv 2 місяці тому
The mutable keyword for lambdas its if you invoke the lambda several times. If you modify lambda copy of parameter - it would affect the next invokation. Thats why you have to explicitly say that its the behavior you want.
@KNT_19
@KNT_19 2 роки тому
Can you please make video explaining the basic differences between function pointer and lamdas?
@DrGreenGiant
@DrGreenGiant 4 роки тому
Could you explain why find_if is any better than writing your own for loop. I know this is a simple example but some of the STL seems a bit superfluous
@michaelwplde
@michaelwplde 3 роки тому
5:00 When I need that I use it ALL THE TIME.
@GamerSaga
@GamerSaga 4 роки тому
got to capture them all! [pokemon].
@cockoroach
@cockoroach 3 роки тому
Lol
@charr6108
@charr6108 2 роки тому
how do I write a function that allows me to pass a lambda into it? do I use template ?
@rj00a
@rj00a 6 років тому
Hey Cherno! What build systems do you use at work?
@jeevanreddy7989
@jeevanreddy7989 4 роки тому
Do you think lambda's can be substitute for a function which huge function body? I mean..what if the call back function has a huge body and not just a one or two lines of code?
@casperes0912
@casperes0912 3 роки тому
What's the lifetime of the closure though? If you capture something in there you lock yourself in a way because if you free the reference the lambda function can't get to it. If you pass something by value I assume the value is taken from the point the Lambda is generated not when it's called, since otherwise the surrounding code and thus the variable could be deallocated from the stack. But since the lambda can be called after the place it's created, it must be heap so when does it get destroyed? Is that up to us? How do we know if whatever we pass it to is done with it? Lambdas in C++ feel like a memory leak landmine to me with the understanding I have at this moment. - Love them in Swift where I feel I understand the ARC system very well to use them in a neatly handled way memory wise, but here I feel like I could very easily leak memory
@chyldstudios
@chyldstudios 5 років тому
do more c++ videos on functional programming
@Arthur-xr5qf
@Arthur-xr5qf 5 місяців тому
"its pretty simple" every lambda can take inputs within the callback parent function's scope, or take inputs in the declaration scope, using the [] part. and the pointer syntax changes with usage. WHAT
@ycombinator765
@ycombinator765 2 роки тому
at this point, I can only hear daughter instead of data. tbh. brilliant stuff
@toby9999
@toby9999 7 місяців тому
Australian accent vs whatever your accent is.
6 років тому
make video about constexpr and std::optional
@fishyperil2153
@fishyperil2153 6 років тому
I'd like a video about constexpr too. I mean, conceptually it isn't hard to understand but it would be great to see it in action with some examples of different situations where you would need to use it
@peterarbeitsloser7819
@peterarbeitsloser7819 3 роки тому
@@fishyperil2153 totally agree
@bulentgercek
@bulentgercek 3 роки тому
Wait, as i look at the handle of the window behind Cherno, it seems that window opens to outside.
@informativecontent4778
@informativecontent4778 6 років тому
U r love
@dani-z3n1th
@dani-z3n1th 2 роки тому
nice couch you got there.
@ashutosh108
@ashutosh108 6 років тому
Missing link to cppreference for lambdas: en.cppreference.com/w/cpp/language/lambda (it was missing in the video description at the moment this comment was posted).
@TheCherno
@TheCherno 6 років тому
Added, thank you! :)
@omarahmedwassel5203
@omarahmedwassel5203 5 років тому
Wow....... thanks a lot ,from egypt .
@mina_ashraf
@mina_ashraf 5 років тому
mnen ya zmala?
@_slier
@_slier 3 роки тому
so, whats the differences between raw function pointer and std::function ?
@m4l490n
@m4l490n 3 роки тому
Why not just only use function pointers? This notation is more confusing and verbose. Could anyone give an example where it would be best using lambdas than function pointers? It seems to me that lamdas are only useful when you want something really simple and only once. Is this the use case?
@jannesopanen8032
@jannesopanen8032 3 роки тому
First of your videos that blew my mind off to another planet. Sorry really you show new syntax there that you haven't told us before... :(
@Babile
@Babile 6 років тому
Hi, have question. Is lambda like this [](){/*...*/)(); when it finishes job stays in memory but you can't access it because no pointer is looking at that function, you didnt assigned name or is auto deleted after job is done?
@rahul-patil
@rahul-patil 3 роки тому
7:12 Can you tell us more about std::function.
@MichaelYoussry
@MichaelYoussry 6 років тому
Idea for next video: Advanced templates
@qaztrewqaz9960
@qaztrewqaz9960 3 роки тому
Does "Capture" thing related to 'Closure' ?
@nitinbindal6617
@nitinbindal6617 3 роки тому
How to pass by const reference in lambdas ? Which might be useful when you pass large objects in lambdas
@SuperSaiyan-tr7fz
@SuperSaiyan-tr7fz 2 роки тому
Apart from Asthetics what is the main advantage of using a lambda over a normal function.
@aakashs1806
@aakashs1806 Рік тому
It is like list comprehension concept in python right?
@TonoNamnum
@TonoNamnum 3 роки тому
Where is the lambda stored? I am using lambdas as callbacks. If declare a lambda in local scope and plan to call that later will that work?
@reubenfrench6288
@reubenfrench6288 6 років тому
Function objects. You said in the last video you were going to make a video on function objects.
@jayayson8549
@jayayson8549 4 роки тому
0:00 Chris? is that you?
@usman8872
@usman8872 Рік тому
you just added "function" and did some magic to make your code work. but forgot to explain what you did. as your "lambda" was having some syntax issue. can you please explain that.
@UrThysis
@UrThysis 4 роки тому
So its kinda like arrow functions in JS ?
@jlewwis1995
@jlewwis1995 4 роки тому
Why does using captures with lambdas not work without std::function?
@be8059
@be8059 5 років тому
Maybe talk about “Traits” ?
@user-qg8hi2th6k
@user-qg8hi2th6k 4 роки тому
auto lambda = [=](int) {std::cout
@user-qg8hi2th6k
@user-qg8hi2th6k 4 роки тому
OK,Thanks!@Peterolen
@maciej12345678
@maciej12345678 2 роки тому
why it by reference not value ? and why you dont using --- using namespace std;
@toopkarcher
@toopkarcher Рік тому
Soooooo a callback function
3 роки тому
What happens if you capture a stack variable by reference and the lambda is ran after the function which created it terminates and therefore dealocated the captured variable?
@toby9999
@toby9999 7 місяців тому
I don't see how that could happen?
@denischikita
@denischikita 2 роки тому
hardest video so far
@andreyalexandrov6
@andreyalexandrov6 6 років тому
Where's that black line at? :D
@user-zi8jn1go8k
@user-zi8jn1go8k Рік тому
So you don't need to specify the return type explicitly for lambdas? What happens if you try to return a different type than what's expected?
@afterlife1578
@afterlife1578 Рік тому
would lead to an error like a normal function
@alexandergonzalez5975
@alexandergonzalez5975 6 років тому
Please make a sophisticated course with some sort of company talking about algorithms/data structures, and sell it to us that would be amazing.
Why I don't "using namespace std"
14:35
The Cherno
Переглядів 383 тис.
Function Pointers in C++
12:41
The Cherno
Переглядів 376 тис.
ПЕЙ МОЛОКО КАК ФОКУСНИК
00:37
Masomka
Переглядів 2,9 млн
😳 Домінація! ОГЛЯД БОЮ ЛОМАЧЕНКО - КАМБОСОС
04:06
Glow Stick Secret 😱 #shorts
00:37
Mr DegrEE
Переглядів 118 млн
Worst Implicit Casting in C++
1:00
The Cherno
Переглядів 67 тис.
Templates in C++
17:58
The Cherno
Переглядів 571 тис.
This Animation System IS AMAZING
19:27
The Cherno
Переглядів 52 тис.
Conversion Operators in C++
19:56
The Cherno
Переглядів 31 тис.
I HATE "premature optimization is the root of all evil" (sometimes)
0:51
I Rewrote This Entire Main File // Code Review
16:08
The Cherno
Переглядів 85 тис.
I REMADE My First Game 12 YEARS LATER!
37:50
The Cherno
Переглядів 24 тис.
BEST WAY to make Desktop Applications in C++
26:00
The Cherno
Переглядів 866 тис.
I ACCIDENTALLY Created Hazel's Greatest Feature
18:21
The Cherno
Переглядів 36 тис.
ПЕЙ МОЛОКО КАК ФОКУСНИК
00:37
Masomka
Переглядів 2,9 млн