Thanks!
Ahh, I see, the first example was compiling, but compiling differently than I expected. I never did test if the bool was being set correctly. I saw it compile, and assumed it worked as intended.
So, if I want to:
* initialize a variable
* and, the setting process is expensive - so I don't want to use a lambda function that runs several times
* and, the setting process can't be done in one line of code
* and I don't want to first initialize it to a default or invalid state than immediately try to re-assign it
* and I have to capture a variable(s)
* and I want the initialization code to be nearby, rather in a separate function
... is this my best option:
1 2 3 4 5
|
int main() {
unsigned long number = 5;
auto setNumber = [&] { return 10!=number; };
bool thisWorks = setNumber();
}
| |
I know this example looks silly, but it's a reduced case scenario. Setting this variable is 1 line of code, so lambda isn't needed, but I'm interested in when it's more than 1 line of code.