1 00:00:00,124 --> 00:00:02,957 (uplifting music) 2 00:00:05,220 --> 00:00:08,370 -: Welcome to this bonus section for the course. 3 00:00:08,370 --> 00:00:10,140 In this section, we'll learn the basics 4 00:00:10,140 --> 00:00:13,155 of C++ plus Lambda expressions. 5 00:00:13,155 --> 00:00:17,640 Lambda expressions were first introduced in C++ 11, 6 00:00:17,640 --> 00:00:21,063 and then extended in C++ 14 and 17. 7 00:00:22,080 --> 00:00:23,820 They provide a very convenient way 8 00:00:23,820 --> 00:00:27,030 to provide functionality exactly where it's needed 9 00:00:27,030 --> 00:00:28,983 without writing lots and lots of code. 10 00:00:29,820 --> 00:00:31,680 In order to understand Lambda expressions, 11 00:00:31,680 --> 00:00:33,960 it's important to understand what they are 12 00:00:33,960 --> 00:00:37,290 and what motivated their addition to C++. 13 00:00:37,290 --> 00:00:38,123 First, we'll look 14 00:00:38,123 --> 00:00:40,697 at what motivated C++ Lambda expressions, 15 00:00:40,697 --> 00:00:44,460 and in order to do that, we'll quickly review using C++ 16 00:00:44,460 --> 00:00:47,250 function objects, or functors. 17 00:00:47,250 --> 00:00:48,540 You may remember that I spoke 18 00:00:48,540 --> 00:00:50,910 about functors in the Algorithms lecture 19 00:00:50,910 --> 00:00:54,273 in the Standard Template Library section of the course. 20 00:00:54,273 --> 00:00:57,450 Why is it important to understand function objects? 21 00:00:57,450 --> 00:00:59,100 Well, because behind the scenes 22 00:00:59,100 --> 00:01:01,615 the C++ compiler generates anonymous 23 00:01:01,615 --> 00:01:06,600 or unnamed functional objects from Lambda expressions. 24 00:01:06,600 --> 00:01:07,560 Then we'll look at the syntax 25 00:01:07,560 --> 00:01:11,490 of a Lambda expression and go through every part of it. 26 00:01:11,490 --> 00:01:12,930 We'll then see the difference 27 00:01:12,930 --> 00:01:16,560 between stateless and stateful Lambda expressions. 28 00:01:16,560 --> 00:01:17,880 Stateless lambdas only know 29 00:01:17,880 --> 00:01:20,370 about the information that's passed into them 30 00:01:20,370 --> 00:01:24,150 via the parameter list, pretty much like a regular function. 31 00:01:24,150 --> 00:01:26,250 We'll then learn about stateful lambdas. 32 00:01:26,250 --> 00:01:27,840 These are a bit more complex 33 00:01:27,840 --> 00:01:29,550 since they can capture the elements 34 00:01:29,550 --> 00:01:31,920 from the environment that they execute in, 35 00:01:31,920 --> 00:01:33,660 so they close around their environment, 36 00:01:33,660 --> 00:01:37,050 and that's where the concept of a closure comes from. 37 00:01:37,050 --> 00:01:39,132 This is a very, very powerful concept 38 00:01:39,132 --> 00:01:42,270 and happens via the Lambda's capture list. 39 00:01:42,270 --> 00:01:43,560 There are lots of options here, 40 00:01:43,560 --> 00:01:46,170 and we'll look at the ones that are most commonly used. 41 00:01:46,170 --> 00:01:50,010 Finally, we'll look at Lambdas in the context of the STL. 42 00:01:50,010 --> 00:01:52,890 We've already seen basic lambdas in the STL section, 43 00:01:52,890 --> 00:01:54,900 but we'll look at a few more examples here. 44 00:01:54,900 --> 00:01:55,770 That's it. 45 00:01:55,770 --> 00:01:57,330 I hope you enjoyed this section. 46 00:01:57,330 --> 00:01:59,340 Please remember, I'm only covering the basics 47 00:01:59,340 --> 00:02:01,440 of C++ Lambda expressions. 48 00:02:01,440 --> 00:02:03,450 They can get very complex when we use them 49 00:02:03,450 --> 00:02:05,700 with templates, as class functions, 50 00:02:05,700 --> 00:02:09,930 and when we pass and return Lambda's to and from functions. 51 00:02:09,930 --> 00:02:12,870 So let's see what motivated C++ Lambda expressions 52 00:02:12,870 --> 00:02:13,770 in the next video.