What that piece of code is doing is essentially the same as defining a function:
1 2 3 4 5 6 7 8 9 10 11
|
static bool init_c(int a, int b)
{
if (a == 1234 || b == 5678)
{
return true;
}
else
{
return false;
}
}
|
and then use it to initialize c.
The advantage of using the lambda is that the initialization code doesn't need to be put into a separate function.
You could off course have written something like this
1 2 3 4 5 6 7 8 9
|
bool c;
if (a == 1234 || b == 5678)
{
c = true;
}
else
{
c = false;
}
|
But some people don't like this because it leaves c uninitialized for a while. If you later restructure the code, or make a mistake in the initialization code (e.g. assign to the wrong variable), you might end up using c in an uninitialized state.
New features often gets overused. Don't get me wrong, lambdas can certainly be useful when initializing variables, but if you just want to give it one of two values you can often use the conditional operator (?:) to accomplish the same thing much more easily.
|
bool c = (a == 1234 || b == 5678 ? true : false);
|
In this particular case we don't even need to use the conditional operator because the || operator already returns a bool, which is what we want, so the code could be written simply as:
|
bool c = (a == 1234 || b == 5678);
|