Hello and welcome to C++ Weekly.
I'm your host, Jason Turner.
I'm available for code reviews, contracting,
and on-site training.
In this episode, I'm going to revisit a previous episode
and tie everything together with the features that we've got
in C++ 20.
So if we go back here to episode 94,
Lambdas as Comparators in C++ 17 mode,
this is where I left off as the best that we can do if we want
to have a concise way of using a lambda as a custom comparator.
And we can do this with our lambda passed in using
the class template argument type deduction from C++ 17.
But I don't love it, because we must give it
at least one value in the set.
And maybe we just wanted to default initialize the set
or something.
So we can do it this way.
And the first comment up here says,
this is the main reason behind p0624r2 stateless lambdas will
be default constructable and assignable in C++ 20.
Well, that's true.
Now we're talking about C++ 20.
Let's see what we can do with it.
So back to this example, and we are in C++ 20 mode.
And we already know it's not just that our lambdas are
default constructable and default assignable,
it is also that they can be used in an unevaluated context.
So let's take this out here, and say we've got a set of my data,
and then we want the comparator to be this lambda.
So we need to put a closed angle bracket there,
and we are getting compile errors which isn't surprising.
Expected initializer before less than.
Oh, well, it's really not surprising,
because I have my code in the wrong order.
There we go.
So now we should be getting an error
that we've got some sort of mismatch here, which
isn't surprising, again.
So we need a type, not a value.
So this is what the combination of these features now gives us
in C++ 20.
I wanted to go back and revisit this old episode,
because I wanted to start showing how things are changing
in the language when I can.
I don't know if this is the most compelling use
case, personally, for lambdas in unevaluated context, default
constructable lambdas, and all that sort of thing.
But we can do something like this,
and the compiler is going to do exactly
for us like it did in the unique pointer episode,
in the last episode, where it just said--
OK, well I know how to default construct
something of this type.
That's your comparator, and we're good to go.
Now of course, with the spaceship operator coming
in C++ 20, this can actually get cleaner again,
particularly for something like this where we're just doing
left side versus right side.
This would be, again, a more compelling example
if we wanted to do it out of order or something like that.
This is what we can do, pulling these features together in C++
20.
So I hope you liked this episode and this wrap up of some
of the more features coming to lambdas in C++ 20.
And these are all things that have been approved,
voted into the standard, and are available with our trunk
compilers at the moment, which is the key point here I forgot
to mention in this episode.
I am using GCC's trunk in C++ 2a mode,
which I would like to take a moment to point out one other
little thing.
If we take out this decal type here, which as we already know
is required, we're going to get a strange error.
Expected a semicolon before greater than token.
That's because it's kind of thinking
that this greater than matches with that less than right
here, so there's perhaps, you know, still some parsing things
to be sorted out with the latest compilers
on this kind of thing.
But if we do it correctly with the decal type,
then you'll get the results that you expect.
But if you see that error when you're playing with it,
just be aware it's because, I believe,
the parser's slightly confused.
So again, thanks for watching and be sure to subscribe.