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?
Aucun commentaire:
Enregistrer un commentaire