Hello, and welcome to C++ Weekly.
I'm your host, Jason Turner.
I am available for contracting, on-site training,
and code reviews.
Now if you've been paying attention lately,
you might have noticed that I have done a lot of episodes
on lambdas.
And you might be like, hey, Jason,
what's with all of the episodes about lambdas?
And that's a pretty good question.
And, well, there actually is a good reason for it.
And I've spent a lot of time with C++,
and with lambdas obviously, and I've come to a realization,
and I thought of something that I would share with the viewers
of C++ Weekly here.
Now I'm using compiler explorer here for, well,
absolutely no particularly good reason
whatsoever just because it's a tool that I happen to have up.
But I'm not actually going to do any programming today,
I'm just going to write some comments.
Lambdas, if you've watched all of the episodes here
and have been paying attention, are shockingly complex
simple things.
So if we have done something like this-- and actually,
maybe I will write some code.
We've just defined a lambda, and this
is a lambda that takes a zero parameters, has zero captures,
and returns void because it uses implicitly auto return type
deduction.
And this is something that has been wrapped
in a class with a handy call operator, or operator,
or paren, whatever you want to call it--
operator overload.
So to understand what this line of code is doing,
you need to have some understanding of C++ classes,
member functions, operator overloading.
And if you really understand how this particular operator
overload that that provides for you works,
you need to understand what is const, what is mutable,
what constexpr is.
And then if we talk about things that we
can apply to our lambda, no accept comes into play.
And I already mentioned return type deduction,
and we also can do trailing return types with our lambda--
so that is trailing return types.
And now going from here, if we were
to add a parameter to this, say, something like that, then we
also need to understand templates, auto,
and template deduction rules, because that will come
into play at some point here--
if I could spell rules.
And all along here, we might be noticing--
even though this disassembly is quite small--
no assembly generated-- that it's not
generating any assembly.
Well, OK, let's put a main function here,
and let's call this L. Now this is with optimizations disabled.
I'm not passing it any values-- let's
go ahead and pass it a value.
Now we can see that it is generating this operator
paren that is-- oh, look, it's a template.
Look we had to know something about templates--
did I mention templates?
I mentioned templates.
And then this automatic template type deduction here.
If we turn on the optimizer, then this
is all going to go away.
And we don't even have to turn the optimizer up
real high, because it's going to get inline--
it's going to go away.
So 01-- it goes away.
So we needed to understand the optimizer.
And I mentioned no except and constexpr, but that's just--
if we really understand how this stuff works, then I mean,
it can be used in a constexpr context as of C++ 17,
but before that it couldn't be.
So that is-- well, let's see-- we're missing something else.
Let's see-- we can do this, and make this a variadic--
so that is.
And with that being a variadic here,
we could probably do something like this.
And then we can actually throw fold expressions into this.
And I mean, it's not necessary to know them--
definitely something that is a natural consequence of using
variadic generic lambdas that were added in C++ 14.
So we've got classes, member functions-- which kind of
implies that you have to understand how functions work--
operator overloading, const, mutable constexpr, no except,
return type deduction, trailing return types, templates,
auto template deduction rules, the optimizer,
variadic templates, fold expressions.
Now I did ask for a little bit of help
when I was preparing this episode
to see if I was missing something,
so I am going to check that and see if I'm
forgetting anything else.
So after checking over my notes here,
there's a couple more things that we might want to look at.
Now I didn't say objects, but--
well, objects.
This lambda called L here, this is an object--
we need to know something about it.
So objects are in here.
And then we've got this interesting case of--
so I am calling a function that returns
an object that's a string.
I have optimizations at level one right now.
Let's turn them up a little bit more
so we get a little bit more inlining,
and that thing goes away again.
So to understand why this works, besides understanding
the optimizer, you need to understand return value
optimization, named return value optimization, what makes--
so I've got fold expressions-- let's see--
RVO-- return value optimization--
named return value optimization.
Those come into play in what makes using immediately invoked
function expressions, or immediately invoked lambda
expressions in C++, actually a very,
very efficient way of composing our code.
And then on top of all this, as Ben Dean pointed out--
that's who I asked for help here--
if you go and look at the standard on lambdas,
it's one of the newest portions of the standard,
so it is a little bit more succinct and easier to read.
It doesn't have as much older baggage with it necessarily.
And then as we move into C++ 20, we're getting more and more
aspects here that are being added to our lambdas,
and we've seen that in the recent C++ 20 episodes that I
have released about lambdas.
So that brings us into things like ODR-used,
and compile time context, constructability.
That just goes on and on.
So I'm reaching about the end here, but at the moment,
we've got what--
22 things that I've listed.
So my argument here that I'm making to you is that fully
understanding lambdas, what they are capable of, following them
through the standard, understanding what changed,
and understanding what they're going to be capable of in C++
20 gives you every single aspect of C++ that is important.
If you fully understand lambdas, you understand every part
of C++ that matters in our real code every day.
And as you can see, it is a long list.
There's maybe a couple of things missing from this,
but not very many.
So to celebrate this, I went ahead and put together
a playlist on the C++ Weekly channel.
So you can go here to my C++ lambdas playlist,
and you can see I currently have 21 videos.
This one will be the 22nd video in this.
I should probably put them in some sort of order
that makes sense, but they go back from episode 32.
So I've been talking about lambdas here pretty much since
the beginning of C++ Weekly, and that's what I have to say.
Check out these episodes, share this list with your friends,
and understand lambdas, and thereby gain a better
understanding of C++.
So thanks for watching this episode--
I hope you enjoyed it.
Be sure to subscribe.