Hello, and welcome to C++ Weekly. I'm your host, Jason Turner. I'm available for code
reviews and on-site training. This is my fourth, and currently my final, planned episode on C++
homework; and this is probably the one that you least saw coming.
So now just for a quick recap I would like to point out that these episodes do not
necessarily represent best practices; these are episodes designed to get you thinking
about C++ and learning new things. Up to this point we have had "'auto' All of The Things,"
"'const' All of The Things," and "'constexpr' All of The Things." Now this episode title, as you
have already seen, is "Lambda All of The Things." Lambdas are interesting and different in C++
in that they are by default 'const' - you must add a mutable keyword to a lambda if
you want to mutate any of its captures. They are also by default implicitly 'constexpr'
enabled; so if you've been following along with some project,
either the smallpt example that I mentioned before or one of your own projects, you are free
to pick up where you last left off or to start from scratch and see how this changes things.
So there's going to be several questions like, "Does it make sense to make the operator "plus"
a lambda?" Probably not, and if you were to take this exercise to its extremes and make a static,
something like this, some sort of constexpr static lambda out there where you implement
this "plus" operation, uh, you can do that by all means and you can see how it changes
the code. Generally speaking, I think you're probably not going to want to make your member
functions into lambdas, but it's certainly something to play with.
Now in the "'const' All of The Things" episode you probably used lambdas at least in some cases
so that you could make values 'const', and this would be building on that; but you can play with
this in a lot of different ways. You can make all of these global functions lambdas you can split
up lots of small portions of this code into lambdas or if you're using your own project
I'm guessing you have more free functions than we do here; but make your free functions lambdas, and
play with making your member functions lambdas, and just kind of see what happens. What happens
when you go too far with lambdas? Or is it possible for you to go too far? It is worth
mentioning that I have suggested the smallpt example for each of these exercises and that
is here. This is the "smallpt: Global Illumination in 99 lines of C++." It generates a 3d scene with
some spheres in it. You can execute it. It is a very small, self-contained example. It is now
over 10 years old, so you know it's going to work and you're going to have fun moving this thing to
C++20. You can get the raw source code here, and if you do this you can just easily copy and paste
this into Compiler Explorer. Make sure you have C++20 turned on. Make sure you've got warnings
enabled because this is going to be a learning experience. You probably want to delete the pragma
for omp, and you probably want to format the document, and, you know what? You probably want
to add some parentheses in some places because, come on now, like, does anyone actually write code
like this? A for loop? There's no open brace here. Without this brace here this entire for loop here
is guarded (not guarded, but as part of this for loop). You know what? Yeah,
you probably want to clean this code up if you start with this code, and if you start with
your own version of it you might be really happy with where you had already gotten. You
might actually like the code that is "const-ed" and "auto-ed" and "constexpr-ed" everywhere,
and maybe you want to make a fork of that code or something to play with lambdas more and see
what happens in your code. This is definitely a much more nebulous, less well-defined exercise
compared to the previous three; but I still think that there's something enlightening here.
Once you realize that you don't have to specify 'constexpr' on a function, you don't necessarily
have to specify 'const' on a function, and it gets some of these defaults more right and it does auto
return type deduction for you by default, then you might actually prefer using lambdas more
places. I don't know, but again I hope this one's fun for you, and I will have a place
in the official C++ Weekly GitHub repository for you to post your examples for how your resulting
code looked when you were done "lambda-ing" all of the things. Thanks for watching this episode
of C++ Weekly. I hope you liked it. I hope you are having fun with these homework assignments. Again,
this is the last one that I have planned, but it's entirely possible more will pop up.