How to immediately invoke a C++ lambda?
A constructor from a class I'm inheriting requires a non-trivial object to be passed in. Similar to this:
MyFoo::MyFoo() : SomeBase( complexstuff )
{
return;
}
The complexstuff
has little to do with MyFoo
, so I didn't want to have to pass it in.
Instead of writing some kind of 1-off temporary function that returns complexstuff
I used a lambda. What took me a few minutes to figure out is I have to invoke the lambda. So my code now looks like this:
MyFoo::MyFoo() : SomeBase(
[]()
{
/* blah blah do stuff with complexstuff */
return complexstuff;
} () )
{
return;
}
If you didn't catch it, it is subtle. But after the lambda body, I had to put ()
to tell the compiler to immediately "run" the lambda. Which made sense after I figured out what I had done wrong. Otherwise, without the ()
to invoke the lambda, gcc says something similar to this:
error: no matching function for call to 'SomeBase(<lambda()>)'
But now that has me thinking -- did I do this correctly? Is there a better way in C++11 or C++14 to tell the compiler that I want it to immediately invoke a lambda I've written? Or is appending an empty ()
like I did the usual way to do this?
Solution 1:
But now that has me thinking -- did I do this correctly?
Yes you did.
Is there a better way in C++11 or C++14 to tell the compiler that I want it to immediately invoke a lambda I've written?
Not that I know of. A lambda is also just a function object, so you need to have a ()
to call it, there is no way around it (except of course some function that invokes the lambda like std::invoke
).
If you want you can drop the ()
after the capture list, because your lambda doesn't take any parameters.
Or is appending an empty
()
like I did the usual way to do this?
Yes, it is the shortest way. As said before, std::invoke
would also work instead, but it requires more typing. I would say a direct call with ()
is the usual way it is done.
Solution 2:
In C++17 you can use std::invoke
. This does the exact same thing as you did, but perhaps you will find this clearer.
#include <iostream>
#include <functional>
void foo(int i)
{
std::cout << i << '\n';
}
int main()
{
foo( std::invoke( []() { return 1; } ) );
}