1 00:00:05,070 --> 00:00:07,650 Welcome to this bonus section for the course. 2 00:00:08,250 --> 00:00:12,540 In this section, we'll learn the basics of C++ Lambda expressions. 3 00:00:13,490 --> 00:00:21,170 Lambda expressions were first introduced in C++ 11 and then extended in C++ 14 and 17. 4 00:00:22,000 --> 00:00:27,730 They provide a very convenient way to provide functionality exactly where it's needed without writing 5 00:00:27,730 --> 00:00:29,020 lots and lots of code. 6 00:00:29,770 --> 00:00:34,900 In order to understand lambda expressions, it's important to understand what they are and what motivated 7 00:00:34,900 --> 00:00:36,790 their addition to C++. 8 00:00:37,180 --> 00:00:42,820 First, we'll look at what motivated C++ lambda expressions, and in order to do that, we'll quickly 9 00:00:42,820 --> 00:00:46,660 review using C++ function objects or functions. 10 00:00:47,170 --> 00:00:52,480 You may remember that I spoke about functions in the algorithm's lecture in the standard template library 11 00:00:52,480 --> 00:00:53,680 section of the course. 12 00:00:54,840 --> 00:00:57,280 Why is it important to understand functional objects? 13 00:00:57,300 --> 00:01:04,170 Well, because behind the scenes, the C++ compiler generates anonymous or unnamed functional objects 14 00:01:04,170 --> 00:01:05,820 from land expressions. 15 00:01:06,570 --> 00:01:10,950 Then we'll look at the syntax of a lambda expression and go through every part of it. 16 00:01:11,750 --> 00:01:16,220 Well, then see the difference between stateless and stateful lambda expressions. 17 00:01:16,430 --> 00:01:21,750 Stateless lambdas only know about the information that's passed into them via the parameter list. 18 00:01:21,770 --> 00:01:23,600 Pretty much like a regular function. 19 00:01:23,990 --> 00:01:26,130 Well then learn about stateful lambdas. 20 00:01:26,150 --> 00:01:31,220 These are a bit more complex since they can capture the elements from the environment that they execute 21 00:01:31,220 --> 00:01:31,730 in. 22 00:01:31,730 --> 00:01:36,080 So they close around their environment and that's where the concept of a closure comes from. 23 00:01:36,920 --> 00:01:41,930 This is a very, very powerful concept and happens via the lambdas capture list. 24 00:01:42,140 --> 00:01:45,770 There are lots of options here and we'll look at the ones that are most commonly used. 25 00:01:46,070 --> 00:01:49,640 Finally, we'll look at lambdas in the context of the STL. 26 00:01:49,880 --> 00:01:54,710 We've already seen basic lambdas in the STL section, but we'll look at a few more examples here. 27 00:01:54,740 --> 00:01:55,640 That's it. 28 00:01:55,760 --> 00:01:57,260 I hope you enjoyed this section. 29 00:01:57,260 --> 00:02:01,300 Please remember I'm only covering the basics of C++ lambda expressions. 30 00:02:01,310 --> 00:02:07,160 They can get very complex when we use them with templates as class functions and when we pass and return 31 00:02:07,160 --> 00:02:09,139 lambdas to and from functions. 32 00:02:09,850 --> 00:02:13,720 So let's see what motivated C++ Lambda expressions in the next video.