Youtube - C++20 Lambdas Familiar Template Syntax - Ben Deane - CppCon 2020
C++20 Lambdas: Familiar Template Syntax - Ben Deane - CppCon 2020
https://youtu.be/uOc6RPu9-CA
Transcrição
[00:09]
00:09 so i wanted to just highlight a little thing about c plus 20 lambdas in 14 we got generic lambdas as you know uh which allows us to template lambda but we only
[00:20]
00:20 get to say auto in the function signature and we have to deal with the value categories in this weird way by decal type and it was cumbersome but in
[00:29]
00:29 cpap plus 20 we got the ability to do what what the paper calls familiar templates syntax lambdas and and i haven't heard a really good adopted name this some people call them template
[00:40]
00:40 lambdas but you know generic lambda is already templates anyway i'm going with what the paper says which is familiar template syntax
[00:48]
00:48 which gives us this template head so we can name the types and get access to the deduce types much more easily okay it's easier so where would we use
[00:58]
00:58 it well consider something like stood apply i know this is a lot of code for this time in the morning for those of you where it is the morning but
[01:06]
01:06 um this is from cpreference.com this is a sample implementation of stood apply we can see that the main body at the bottom all it does is make the index sequence and then
[01:17]
01:17 delegate to the helper function well with familiar template syntax lambdas we can collapse these two functions and make an immediately invoked familiar
[01:27]
01:27 template syntax lambda or fts iilly because we like these things in c plus we like long acronyms and you can see the lambda captures by
[01:37]
01:37 reference and inside the lambda we just do all the same forwarding but it's much more compact much more uh you know you need less code you've got less identifiers you're exposing
[01:50]
01:50 so here's another example which is uh for each on tuples if you just want a simple unary for each on tuples you can get that using a regular invocation of stood apply
[02:01]
02:01 uh like this with the with the familiar template syntax lambda having a uh pack and then forwarding the thing to each folding each thing in the alpaca to the function
[02:12]
02:12 so this is for a unary function but if you want a binary function over two tuples or potentially an energy function over more tuples you need to do a little bit more work
[02:22]
02:22 and uh jonathan bacara had a blog post on this called stl algorithms on tuples on fluentcpp.com a while back and you can see it uses the same kind of
[02:34]
02:34 pattern as did apply so the the bottom function again is the function that the user calls and all it does is forward the tuple each each tuple and the function and
[02:46]
02:46 then it does the make index sequence and in this case we're assuming that the two tuples are the same size but we probably have a a static assert in there probably that
[02:54]
02:54 the tuple size of each tuple was the same which i've left out of this slide code uh but again you can see it's the same pattern as apply we're just
[03:03]
03:03 we're destructuring the type just to just to call this ancillary function the the impul function or the function inside a detail namespace or whatever
[03:11]
03:11 and that's what actually does the work so we can do the same thing here um make it a uh immediately invoked lambda with the new template head uh in this case
[03:25]
03:25 we are using non-type template arc pack of size ts because that's what we get in the index sequence and so we're just rather than having two functions
[03:35]
03:35 one of which just exists to destructure the type we can just stick the lambda in and get the familiar template syntax immediately invoked lambda expression
[03:46]
03:46 and again the reference capture kind of means that we can uh it takes care of the the the forwarding for us we just forward inside the the lambda exactly the same as we
[03:57]
03:57 would otherwise forward and the reference capture takes care of the value category preservation with that forwarding so that's it that's a highlight on
[04:09]
04:09 familiar template syntax lambdas in c plus 20. if you find yourself delegating to a function just for the purpose of destructuring a
[04:16]
04:16 type like this particularly it happens with tuples i find although also with um things like uh boost mp list boost mp11 mp list type lists um then consider this
[04:27]
04:27 pattern consider just sticking in the immediately invoked lambda with the familiar template syntax uh it's a lot less
[04:34]
04:34 visual noise uh it's simpler argument handling the reference capture kind of gives you takes away all that forwarding that you have to do and leaves it just the one
[04:42]
04:42 place and like i say it's especially useful for tuples so i hope you don't hate the acronym but there you go thank you
[05:13]
05:13 you st