Your first example works not how you want, it's equivalent to while (true)
, since lambda will be converted to function-pointer, that will be converted to bool (true) - it should be
while([&]()
{
return true;
}())
Note to call of lambda
Your second example will not compile without call of lambda, since you are trying to access to catched-variables, thats forbids conversion from lambda to function-pointer, that can be converted to bool, but it will neither compiled with ()
,
If
a lambda-expression does not include a trailing-return-type, it is as if the trailing-return-type denotes the
following type:
— if the compound-statement is of the form
{ attribute-specifier-seqopt return expression ; }
the type of the returned expression after lvalue-to-rvalue conversion (4.1), array-to-pointer conver-
sion (4.2), and function-to-pointer conversion (4.3);
— otherwise, void.
In your case, return type will be deduced to void
, but since you return bool, you should use trailing-return-type
while([&]() -> bool
{
std::cin >>lastName;
return true;
}())