Lambdas in C++

  Рет қаралды 331,888

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

Пікірлер: 231
@satyamraj2779
@satyamraj2779 3 ай бұрын
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..
@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!
@dsedchenko
@dsedchenko 5 жыл бұрын
The best cpp video tutorials I've ever seen, thank you!
@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 6 жыл бұрын
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; }
@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.
@jinyoungpark3277
@jinyoungpark3277 5 жыл бұрын
THANK YOU SO MUCH for this video!! You made everything clear
@GeekyMino
@GeekyMino 6 жыл бұрын
Dude your channel is awesome! Your videos are super informative and useful. Keep up the good work :)
@vegansynths7757
@vegansynths7757 4 жыл бұрын
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. :)
@Kazetomosuki
@Kazetomosuki 3 жыл бұрын
Wow, this video in combination with the previous one about function pointers was an enlightenment to me! 😄🙏👏 Thank you!
@dimasveliz6745
@dimasveliz6745 3 жыл бұрын
Thanks for the clarity of video! Especially for the second part that includes the functional thing!
@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.
@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
@jianiliao7140
@jianiliao7140 6 жыл бұрын
Thank you for making the video!
@susiana5
@susiana5 6 жыл бұрын
Thanks for uploading :D nice!
@samitkapoor53
@samitkapoor53 3 жыл бұрын
Thank you, this was so helpful!!
@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 2 жыл бұрын
@@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.
@oatmealduck7937
@oatmealduck7937 4 жыл бұрын
These are so fun to watch 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
@Rahulsingh-theraha
@Rahulsingh-theraha 9 ай бұрын
honestly a great series for programmer who have past exp , did whole thing (till lambdas)in like 3-4 hrs and understood almost everything .
@EezySeven
@EezySeven 6 жыл бұрын
You should make a video on C++ iterators, dude.
@gvcallen
@gvcallen 4 жыл бұрын
please!
@greatbullet7372
@greatbullet7372 3 жыл бұрын
@@gvcallen he did today ;)
@mangrubengaming4300
@mangrubengaming4300 4 жыл бұрын
You really know what you're doing. You're so good. Do you have a video about ECS?
@christianm4906
@christianm4906 5 жыл бұрын
Man, you saved my day.
@sankar9483
@sankar9483 5 жыл бұрын
Thanks lot. Wonderful Video.
@Kapil_Thakar
@Kapil_Thakar 2 жыл бұрын
u are my favorite, Amazing, u kept it very clear...
@joushuahung4996
@joushuahung4996 3 жыл бұрын
Top quality tutorial, Thank you.
@vertigo6982
@vertigo6982 5 жыл бұрын
One can never have too many function pointer, functor, and lambda videos.
@kelvin31272
@kelvin31272 10 ай бұрын
Thank you Cherno! Finally understand it
@shanielsamadhan979
@shanielsamadhan979 6 жыл бұрын
Start the game engine series soooon please. I'm excited af!
@austinbryan6759
@austinbryan6759 6 жыл бұрын
2:24 This is almost the exact case I was wanting to use this for, funny enough
@boody7665
@boody7665 5 жыл бұрын
awesome , Thank you
@alexsece
@alexsece 5 жыл бұрын
Thank you!
@mryup6100
@mryup6100 2 жыл бұрын
This content ► Amazing explanations
@DUFLEBIN
@DUFLEBIN 6 жыл бұрын
Hey great playlist on c++. Can you make a video on iterators in c++?
@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.
@asajjad4542
@asajjad4542 3 жыл бұрын
You're amazing!
@madhvishrivastava3646
@madhvishrivastava3646 2 ай бұрын
Thank you for explaining that complex topic in a very simple way. A big thanks from India.😊
@ahmedanwar976
@ahmedanwar976 5 жыл бұрын
Thanks man
@AbhishekKumar1902
@AbhishekKumar1902 5 жыл бұрын
The best tutorial on the internet!
@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 11 ай бұрын
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.
@achrafbahloul192
@achrafbahloul192 2 жыл бұрын
so helpful thanks
@mrmrigank7154
@mrmrigank7154 3 ай бұрын
best cpp and one of best programming channel ever
@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.
@tomdapchai
@tomdapchai 9 ай бұрын
thank you
@LeSaboteur3981
@LeSaboteur3981 3 жыл бұрын
great, helps a lot!
@DawitBeza-nr1tn
@DawitBeza-nr1tn 3 ай бұрын
you know the way you teach me is exactly the way i want to be tought
@Codeaholic1
@Codeaholic1 5 жыл бұрын
Great video. The couch made me laugh.
@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
@abhishekchavhan3768
@abhishekchavhan3768 Жыл бұрын
Amazing
@ashutoshaswal
@ashutoshaswal 4 жыл бұрын
THANKS
@Muhammad_Abdullah_Sultan
@Muhammad_Abdullah_Sultan Жыл бұрын
Thanks
@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++.
@informativecontent4778
@informativecontent4778 6 жыл бұрын
U r love
@pavliv
@pavliv 4 ай бұрын
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.
@GamerSaga
@GamerSaga 4 жыл бұрын
got to capture them all! [pokemon].
@cockoroach
@cockoroach 4 жыл бұрын
Lol
@KNT_19
@KNT_19 2 жыл бұрын
Can you please make video explaining the basic differences between function pointer and lamdas?
@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
@ycombinator765
@ycombinator765 2 жыл бұрын
at this point, I can only hear daughter instead of data. tbh. brilliant stuff
@toby9999
@toby9999 9 ай бұрын
Australian accent vs whatever your accent is.
@chyldstudios
@chyldstudios 5 жыл бұрын
do more c++ videos on functional programming
@arod3295
@arod3295 2 жыл бұрын
bless yo intelligent, puzzle solving soul
@dani-z3n1th
@dani-z3n1th 2 жыл бұрын
nice couch you got there.
@michaelwplde
@michaelwplde 4 жыл бұрын
5:00 When I need that I use it ALL THE TIME.
@Arthur-xr5qf
@Arthur-xr5qf 6 ай бұрын
"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
@mba2ceo
@mba2ceo 8 ай бұрын
What makes him a great teacher. 1. Confuses the HELL 2. then explains everything 3. GOD DAMN MY PROFESSORS
@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... :(
@rj00a
@rj00a 6 жыл бұрын
Hey Cherno! What build systems do you use at work?
@omarahmedwassel5203
@omarahmedwassel5203 5 жыл бұрын
Wow....... thanks a lot ,from egypt .
@mina_ashraf
@mina_ashraf 5 жыл бұрын
mnen ya zmala?
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
@MichaelYoussry
@MichaelYoussry 6 жыл бұрын
Idea for next video: Advanced templates
@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
@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?
@reubenfrench6288
@reubenfrench6288 6 жыл бұрын
Function objects. You said in the last video you were going to make a video on function objects.
@charr6108
@charr6108 2 жыл бұрын
how do I write a function that allows me to pass a lambda into it? do I use template ?
@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! :)
@m4l490n
@m4l490n 4 жыл бұрын
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?
@nitinbindal6617
@nitinbindal6617 3 жыл бұрын
How to pass by const reference in lambdas ? Which might be useful when you pass large objects in lambdas
@bulentgercek
@bulentgercek 4 жыл бұрын
Wait, as i look at the handle of the window behind Cherno, it seems that window opens to outside.
@be8059
@be8059 5 жыл бұрын
Maybe talk about “Traits” ?
@bloodwolf8307
@bloodwolf8307 4 жыл бұрын
cool
@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.
@toopkarcher
@toopkarcher Жыл бұрын
Soooooo a callback function
@denischikita
@denischikita 2 жыл бұрын
hardest video so far
@jayayson8549
@jayayson8549 4 жыл бұрын
0:00 Chris? is that you?
@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
@_slier
@_slier 4 жыл бұрын
so, whats the differences between raw function pointer and std::function ?
@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?
@SuperSaiyan-tr7fz
@SuperSaiyan-tr7fz 2 жыл бұрын
Apart from Asthetics what is the main advantage of using a lambda over a normal function.
@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?
@chriswhisker
@chriswhisker 19 күн бұрын
What's the difference between capturing by reference and using "mutable" when capturing by copy?
@yadavdhakal2044
@yadavdhakal2044 4 жыл бұрын
Cherno is born for C and C++
@qaztrewqaz9960
@qaztrewqaz9960 3 жыл бұрын
Does "Capture" thing related to 'Closure' ?
@UrThysis
@UrThysis 4 жыл бұрын
So its kinda like arrow functions in JS ?
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 9 ай бұрын
I don't see how that could happen?
@novictim
@novictim 3 жыл бұрын
*googles lambda c++* *presses video* *finds out it is THE CHERNO 🥰😘😘😘🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰*
@computerprogrammer7942
@computerprogrammer7942 3 жыл бұрын
The same reaction when you realise that I’m commenting on your comment
@phantomstriker7996
@phantomstriker7996 Жыл бұрын
A lambda is an anonymous function. A way for us to make a function without having to physically make one.
@aakashs1806
@aakashs1806 Жыл бұрын
It is like list comprehension concept in python right?
@rahul-patil
@rahul-patil 3 жыл бұрын
7:12 Can you tell us more about std::function.
@chinmayanand896
@chinmayanand896 6 жыл бұрын
I want help cherno...I want classes I mean live face to face..guide me how I can get that from you
@emilamenda9649
@emilamenda9649 6 жыл бұрын
Join his discord: thecherno.com/discord
Why I don't "using namespace std"
14:35
The Cherno
Рет қаралды 387 М.
Function Pointers in C++
12:41
The Cherno
Рет қаралды 381 М.
⬅️🤔➡️
00:31
Celine Dept
Рет қаралды 50 МЛН
DO YOU HAVE FRIENDS LIKE THIS?
00:17
dednahype
Рет қаралды 22 МЛН
Мы никогда не были так напуганы!
00:15
Аришнев
Рет қаралды 3,9 МЛН
Should you learn C++?? | Prime Reacts
20:29
ThePrimeTime
Рет қаралды 323 М.
I Tried JAI, Can It Replace C++?! (Programming Language)
21:05
Conversion Operators in C++
19:56
The Cherno
Рет қаралды 35 М.
Stack vs Heap Memory in C++
19:31
The Cherno
Рет қаралды 554 М.
ITERATORS in C++
17:09
The Cherno
Рет қаралды 202 М.
Type Punning in C++
13:20
The Cherno
Рет қаралды 154 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2 МЛН
⬅️🤔➡️
00:31
Celine Dept
Рет қаралды 50 МЛН