"i and g are read-only because we're passing by value to the body of our lambda." That's not _quite_ right. A lambda with a capture list is actually a functor whose `operator()` is const. When a variable is captured by value, it becomes a data member of the functor. Because const functions can't modify their data members (by default), then you can't modify variables captured by value. A reasonable person might then think that capturing by reference would capture by const reference. But no. Someone decided that capturing by reference would be done non-const. There is a way to allow const member functions to modify their data members: by marking them _mutable_. You can also do this with lambdas. int i = 3; auto f = [i]() mutable { i = 4; return i; }; std::cout
A great tutorial that builds gradually from the very basic to the more complex syntax, explaining all the aspects of lambda functions so clearly. Thank you!
"And You are all bunch of Legends" touched my Heart❤️ Thank You Sir I have been trying to learn lambda functions from websites and didn't understand a single word A teacher is really Important Thanks a Lot again ❤️
In your "simplest lambda", I think you can omit the parenthesis and make it even simpler: []{}; (Works in Visual Studio at least, but I think this is standard).
You're the good legend here Creel :D Awesome high-quality content, generally detailed low-level explanations, and cool Aussie accent. Gotta love this channel! Wish I could be a supportive patron, but can't from here. :/ Salutes from Syria anyways. :P (The hype of "Return of Lambda" is unprecedented). xD
+What's a Creel? No one who uses using namespace std; is a legend... Not only that but he does using namespace std, and then still just uses std::vector ...??
Hey mate, nicely done. I really enjoyed how you typed kjhgycikjhgfckiuy for explaining the definition of other code somewhere else :) One nit I did find, when you said end() provides a pointer to the (last) element; this isn't quite true :) I'm sure you know that end() returns one past the last element in the vector ;-) Still, great video; enjoyed it immensely! Happy New Year, BTW!
Thank you for a clear and lucid introduction to lambdas. However, I still don't see the benefit of lambdas. The additional complexity is not offset by the convenience. In fact, defining the function in-line complicates the flow of logic. Defining the lambda as a separate function, the old-fashion way, would improve code legibility.
The expression "using namespace std" is an abomination created in the textbooks and should be avoided. The problem is, for those who may not know, the using expression makes ALL of the symbols within the referenced namespace local to the context enclosing the using clause. If you happen to do this for multiple namespaces, you can get some weird collisions that are hard to diagnose. This possibility is eliminated by just not employing the using clause and explicitly specifying the namespace containing the symbol being referenced. In the code here that would mean writing "std::cout
If you started your program without debugger (ctrl + F5), it would take a lot less time to start up. Also you could omit the breakpoint at the end, because the console stays open after that until you press enter. (at least in visual studio 2015)
Can you write lambdas that evaluate at compile time or static time, such as for initializing constants and static globals? There's a wonderful thing in the Eiffel language called "once function", which is any function pre-attributed with the keyword 'once', that only compute their output the first time they are called, but on subsequent calls return the same value they originally computed. They are a great replacement for global constants. In C-ish notation, once size_t bus_cycles_per_cache_line(){ cache_line_sz() / bus_width(); } //in some other file... once size_t cache_line_sz(){ cpuid(...); } once size_t bus_width(){ whatever(...); } It forces these evaluations to happen in the right order, as opposed to C/C++ which clearly states that the order of evaluation of static initializations is undefined. So these are lazy static initializations, at run-time for the most part, really; but they may be called as early as at compile time if a result is needed at compile time. I've often wondered if there's a way to replicate once functions in C++.
Thank you for your time, and a well explained lecture. Could you please explain in more detail why the ampersand(&) in front of the variable point (point&) in the sort algorithm by the lambda fragment of the code like this [ ](const point& a, const point& b). And also the purpose of the const.
loving your tutorials hope to see more of C++ I have a question though does lamda function capture only the variables declared over lamda function definition or does it capture all of the variables of the scope where lamda function is defined?
Hello! I'm facing some problems with lambda, I need to declare a function who execute something different depending on the input, on the other programs of the project this function is being called like this: int_op["add"](3, 5), but Lamba isn't supposed to be named and they are calling that way, how can I implement this int_op function?? please, help
I don't really understand why you say that the lambda function has no name. In the first example isn't "sayHelloWorld" acting like a name to the lambda?
Can you tell me what use is Lambda's when you could easily just make a function or method instead. I understand how they are used now thanks, just can't understand their use if you can use a function
They're just a different way to do things. They don't offer anything new, but they're pretty convinient soemtimes. I think the best examples of how powerful they are is when you look at processing lists, like filtering of perfomring some operation on the values. I hoep that makes some sense...?
This video is really good. Thank you very much. The only thing that I would like to have to know after this video is whether anonymous functions have any overhead comparing to classic approach. Because C++ is a great language performance-wise and using these fancy tricks by paying some performance cost would be just gay. Also I have subscribed to your channel. Many interesting videos on C++ I see. Because I am studying C++ at the moment and when you study new programming language it is really important to see someone having fun with it which you totally do, sir. But more importantly I am fading away from such politicaly incorrect languages as Java and ActionScript. If you heard some stories about garbage collection and virtual machine performance issues then you know what I mean.
Capture list takes values when lambda is defined, but parameter is given when you are calling the function. So when you call the same lambda, you can pass different params, but capture list is always the same.
Hahaha, the indentation in this vid is a mess! I normally use Whitesmith. It's not popular, but it's super easy to read. But I have to save space on the slides, so I used like K&R. Then I mixed the two together because I'm a moron :S Sorry if it was confusing, hope the vid still made sense and thanks for watching!
Lambda if not used properly produces more mess than good. I acknowledege its existance but wont use it . It is in contrarty to what the concept of OO seems to be. why would you wanna have a function within main? it is turning this language into something worse than assmembler.... however tutorial was excellent
Not every problem needs to be OO-style. In some cases, it's just a waste of time and work to try converting it into that style. Dismissing a tool can only hurt you as a programmer.
It is just about making harder to read.. I don't think there is any performance benefit of this feature. Rewriting you old code and making it harder to read. That is all about .. in my opinion. Many new CPP technique is like so.. It is not really a new thing than making it harder to read.
I haven't coded in a while. I saw it in a coding conference and raised flags on me. I think it is an abomination. It infringes on many coding conventions. Conventions that were specifically agreed upon to prevent loosely written code that ends up churning useless asm lines. Mind you that this is only churning at compile time, but still, convention says changing one line or adding few years to this code, compile is no longer guaranteed. I think behavior wouldn't be guaranteed at all. What strikes me the most, people who are in position to make changes to compilers forget that there are people in this world that are not just software oriented but also hardware oriented. This is why I think people making changes to the compiler are not fit to make changes at all. I know being old fashioned has no place in the future but just saying you know. I'm old. I don't like this new change because lambdas are imposing suggestions that infringe on conventions.
You don't like lambdas? Yes, they are a little strange for C++. The syntax is probably my least favourite thing about them. But, they're really fun and fairly useful time savers sometimes, so it's ok to get used to them. Thanks for watching anywho, have a good one!
Best video for Lambda functions. I wasted a lot of time with other articles and videos and finally came here. Your explanation is awsome.
Agree, a good one,
Another one I liked
ua-cam.com/video/mWgmBBz0y8c/v-deo.html
Agree
Thank you sir for posting the only lambda tutorial that makes any sense whatsoever, finally
This is absolutely the clearest video for explaining the lambda function in C11! 😃
""The return of the Lambda", haha sounds like a horror film." HAHAHA, love your tutorials!
The Silence Of The Lambdas :D
i was gonna type this
OH! This is an amazing video ever made on c++11 Lambdas...All my doubts about Lambdas got resolved today. Thank You great man.
"i and g are read-only because we're passing by value to the body of our lambda."
That's not _quite_ right. A lambda with a capture list is actually a functor whose `operator()` is const. When a variable is captured by value, it becomes a data member of the functor. Because const functions can't modify their data members (by default), then you can't modify variables captured by value. A reasonable person might then think that capturing by reference would capture by const reference. But no. Someone decided that capturing by reference would be done non-const. There is a way to allow const member functions to modify their data members: by marking them _mutable_. You can also do this with lambdas.
int i = 3;
auto f = [i]() mutable { i = 4; return i; };
std::cout
+communistgoatboy Brilliant! Thanks for sharing.
best lamda function explanation on the tube yet
A great tutorial that builds gradually from the very basic to the more complex syntax, explaining all the aspects of lambda functions so clearly. Thank you!
best lambda explanation on the internet, thanks
Man, you just made my day... I've been trying to pass a complicated line of code into a std::thread for days now, and the Lambda function solved it.
Best Lamda introduction I've found, very helpful. Thank you!
"And You are all bunch of Legends" touched my Heart❤️
Thank You Sir
I have been trying to learn lambda functions from websites and didn't understand a single word
A teacher is really Important Thanks a Lot again ❤️
Amazing !
Certainly the best introduction to C++ lambda functions, both in breadth of coverage and good humor.
Understanding lamba is made very easy here. Thumps up for posting the video.
In your "simplest lambda", I think you can omit the parenthesis and make it even simpler:
[]{};
(Works in Visual Studio at least, but I think this is standard).
amazing toturial finally somebody explains clear and with good examples!! thank you for the order man!
extremely informative tutorial video and extremely awesome way to deliver the lecture. Lambda functions suddenly seem so tameable to me now. Thanks. 😊
Welcome, thanks for watching!
Clearest video ive watched on this thank you
Great video. You pushed me over the threshold. I'm finally understand lambdas. Thank you!
4:24 "The Return of The Lambda" , I just explode in laughter, the most epic things it can be heard on your channel ...
You're the good legend here Creel :D
Awesome high-quality content, generally detailed low-level explanations, and cool Aussie accent. Gotta love this channel!
Wish I could be a supportive patron, but can't from here. :/ Salutes from Syria anyways. :P
(The hype of "Return of Lambda" is unprecedented). xD
+Major O Thanks, you're a hero!
+What's a Creel? No one who uses using namespace std; is a legend... Not only that but he does using namespace std, and then still just uses std::vector ...??
+Amanuel Bogale Classy! Can I get you some salsa to have with that chip on your shoulder?
Seriously though, thanks for watching, and have a good one!
What's a Creel? i would actually love that. As One Of my Favorite(Eat it almost all the time) is Salsa!
This is a great video. You broke it down perfectly. Could you please do a series on systems programming
Outstanding simplicity and informative. Top notch!
Thanks, this really helped me understand. Wasn't as hard as I initially thought
12:10 This made me laugh harder than I care to admit..
xxxxxxxxxxxD true
I know right! He did that "asdfasfdasdfasfd" thing a few times which cracked me up every time.
And this Guys is what is *Ultra instincts !!*
.
.
.
15:17 *Ultra Instincts !!*
yeah, it got me too, but when I rewatched it :) first time I was like: well that was a general example... :D
Stroke Oriented Programming
I laughed way too hard aswell, it's like getting your mate showing you some programming tricks. Laid back dude!
Return of the Lambda
Very nice video, you made it fun to learn it, thanks for it😁
Thanks man,cool explanation about lambdas.
Hey mate, nicely done. I really enjoyed how you typed kjhgycikjhgfckiuy for explaining the definition of other code somewhere else :)
One nit I did find, when you said end() provides a pointer to the (last) element; this isn't quite true :) I'm sure you know that end() returns one past the last element in the vector ;-)
Still, great video; enjoyed it immensely!
Happy New Year, BTW!
Good tutorial for cpp lambda, thank you
love the way of teaching and some interesting dialogue in the middle made me laugh hahaha!!
It's very useful and easy to understand.
your tutorial is so good. clear fast and direct. really helpfull thanks!
Yeah, thanks man. Very good videos.
Thank you for this video.
very detail explanation. thank you
Very nicely put, kudos
Amazing video, thank you so much.
Thank you for a clear and lucid introduction to lambdas. However, I still don't see the benefit of lambdas. The additional complexity is not offset by the convenience. In fact, defining the function in-line complicates the flow of logic. Defining the lambda as a separate function, the old-fashion way, would improve code legibility.
Excellent video
The expression "using namespace std" is an abomination created in the textbooks and should be avoided. The problem is, for those who may not know, the using expression makes ALL of the symbols within the referenced namespace local to the context enclosing the using clause. If you happen to do this for multiple namespaces, you can get some weird collisions that are hard to diagnose.
This possibility is eliminated by just not employing the using clause and explicitly specifying the namespace containing the symbol being referenced. In the code here that would mean writing "std::cout
dude i love your videos
If you started your program without debugger (ctrl + F5), it would take a lot less time to start up. Also you could omit the breakpoint at the end, because the console stays open after that until you press enter. (at least in visual studio 2015)
+bestraub98 Cheers! Thanks for the info.
iterator end() points to one element past the end, not to the last element
Perfect, easy explanation. Thank you.
I think this shit is what Bjarne Stroustrup meant when he said "With C++ you can blow your whole leg off."
great tutorial
Good tutorial!
Very informative !!!
Thank you! You're a great teacher
Can you write lambdas that evaluate at compile time or static time, such as for initializing constants and static globals?
There's a wonderful thing in the Eiffel language called "once function", which is any function pre-attributed with the keyword 'once', that only compute their output the first time they are called, but on subsequent calls return the same value they originally computed. They are a great replacement for global constants. In C-ish notation,
once size_t bus_cycles_per_cache_line(){ cache_line_sz() / bus_width(); }
//in some other file...
once size_t cache_line_sz(){ cpuid(...); }
once size_t bus_width(){ whatever(...); }
It forces these evaluations to happen in the right order, as opposed to C/C++ which clearly states that the order of evaluation of static initializations is undefined.
So these are lazy static initializations, at run-time for the most part, really; but they may be called as early as at compile time if a result is needed at compile time. I've often wondered if there's a way to replicate once functions in C++.
How does foreach 'know' what to pass to the lambda, in this case int x. Expressed alternatively, how is int x being assigned?
you are the best
Excellent explanation, thank you!
Thank you for this.
2:45
- Uuuuuu... What does this lambda do?
- It compiles.
Thank you for your time, and a well explained lecture. Could you please explain in more detail why the ampersand(&) in front of the variable point (point&) in the sort algorithm by the lambda fragment of the code like this [ ](const point& a, const point& b). And also the purpose of the const.
'&' pass by reference, instead of by value. Reduces the size of the stack if it's an object.
'const': the function can't change the parameter.
here in this lambda function we are passing [] (int x) what is x and how internally it is working.please elaborate it as i am new to this.
Thank you Obi One Kenobi :D
loving your tutorials
hope to see more of C++
I have a question though
does lamda function capture only the variables declared over lamda function definition or does it capture all of the variables of the scope where lamda function is defined?
Probably only over, as otherwise you would be referring to a variable that doesn't exist
Hello! I'm facing some problems with lambda, I need to declare a function who execute something different depending on the input, on the other programs of the project this function is being called like this: int_op["add"](3, 5), but Lamba isn't supposed to be named and they are calling that way, how can I implement this int_op function?? please, help
Great explanation! Thanks!
A new tool for obfuscation! It looks to be a way of doing functional programming in C++ but it has a lot of potential for abuse!
In last example ( 18:22 ) what is the difference between (function f ) and (function &f ). Basically, when should i use '&f' and when just 'f'
&f and f can be used interchangeably. typing &f is a good practice though.
Can u make video on recursive lamda using y_combinator
brillaint guy/video, loved it
The best
When I tried running the first program, it just says:
q.cpp:10:7: error: ‘sayHelloWorld’ does not name a type
Why is it that C++ seems to break encapsulation at every opportunity?
I don't really understand why you say that the lambda function has no name. In the first example isn't "sayHelloWorld" acting like a name to the lambda?
thanks..
Day saver
Can you tell me what use is Lambda's when you could easily just make a function or method instead. I understand how they are used now thanks, just can't understand their use if you can use a function
They're just a different way to do things. They don't offer anything new, but they're pretty convinient soemtimes. I think the best examples of how powerful they are is when you look at processing lists, like filtering of perfomring some operation on the values.
I hoep that makes some sense...?
wait this is cool
This video is really good. Thank you very much. The only thing that I would like to have to know after this video is whether anonymous functions have any overhead comparing to classic approach. Because C++ is a great language performance-wise and using these fancy tricks by paying some performance cost would be just gay.
Also I have subscribed to your channel. Many interesting videos on C++ I see. Because I am studying C++ at the moment and when you study new programming language it is really important to see someone having fun with it which you totally do, sir. But more importantly I am fading away from such politicaly incorrect languages as Java and ActionScript. If you heard some stories about garbage collection and virtual machine performance issues then you know what I mean.
can you pls explain the Operator Overloading ,Element & globale Funktion
The syntax in your first few slides does not have the "=" sign after the name. Are they incorrect ?
epic video mate, very informative + i lol'd several times :P
Nice job :)
So you're that youtuber the other channel was asking about!
Not sure mate, what channel?
Thanks for watching :)
@@WhatsACreel The channel named "What's a Creel?"
@@commentor5479 Hahaha, that's gold! I fell for that hook line and sinker :)
Wait. You're the same channel.
@@commentor5479 Yep, just changed the name :)
in place
impressed , by the way, which accent is this speaker.
Thanks for watching. I'm Australian :)
very helpful thx :)
shouldn't the int [&](int x) be [&](int& arr) 12:52 ?
what is the difference between capure list ( "[=]" ) and passing by value in paramter list. ?
Capture list takes values when lambda is defined, but parameter is given when you are calling the function.
So when you call the same lambda, you can pass different params, but capture list is always the same.
make another video please! :D
Easy as Pi😄
15:17 *Ultra Instincts !!*
Great video, but I cannot get over the lack of indentation. (0-e)
Hahaha, the indentation in this vid is a mess! I normally use Whitesmith. It's not popular, but it's super easy to read. But I have to save space on the slides, so I used like K&R. Then I mixed the two together because I'm a moron :S Sorry if it was confusing, hope the vid still made sense and thanks for watching!
What's a Creel? It was definitely helpful, I loved how quickly you move through it. :)
I don't like the naming of std since it also means "Sexually Transmitted Desease"
I guess there's a reason for the guys saying "stood" instead of "ess tee dee" when talking about "std::".
Good video though, thanks!
I think because std usually stands for sexually transmitted disease
can you tell C# ?
Eric Idle, is that you ?
Hahaha, being likened to a Python is the greatest honor of all!!
Lambda if not used properly produces more mess than good. I acknowledege its existance but wont use it . It is in contrarty to what the concept of OO seems to be.
why would you wanna have a function within main? it is turning this language into something worse than assmembler.... however tutorial was excellent
Not every problem needs to be OO-style. In some cases, it's just a waste of time and work to try converting it into that style. Dismissing a tool can only hurt you as a programmer.
It is just about making harder to read.. I don't think there is any performance benefit of this feature. Rewriting you old code and making it harder to read. That is all about .. in my opinion.
Many new CPP technique is like so.. It is not really a new thing than making it harder to read.
I REALLY don't see or appreciate the value of lambda functions. All it does is make your code less readable. Can C++ "live"without Lambda Functions?
Do you like to drink?
I think do you be nice alcoholism companion
I haven't coded in a while. I saw it in a coding conference and raised flags on me. I think it is an abomination. It infringes on many coding conventions. Conventions that were specifically agreed upon to prevent loosely written code that ends up churning useless asm lines. Mind you that this is only churning at compile time, but still, convention says changing one line or adding few years to this code, compile is no longer guaranteed. I think behavior wouldn't be guaranteed at all. What strikes me the most, people who are in position to make changes to compilers forget that there are people in this world that are not just software oriented but also hardware oriented. This is why I think people making changes to the compiler are not fit to make changes at all. I know being old fashioned has no place in the future but just saying you know. I'm old. I don't like this new change because lambdas are imposing suggestions that infringe on conventions.
You don't like lambdas?
Yes, they are a little strange for C++. The syntax is probably my least favourite thing about them. But, they're really fun and fairly useful time savers sometimes, so it's ok to get used to them. Thanks for watching anywho, have a good one!
Good Tutorial.