What I\'m trying to do is eat exceptions when constructing an object that may be invalid. It\'d be perfect for use of std::optional
, but I don
The second snippet runs into [dcl.spec.auto]/10:
If the type of an entity with an undeduced placeholder type is needed to determine the type of an expression, the program is ill-formed.
The type of foo
is needed to determine the type of the expression foo
within the lambda body, but at that point you haven't deduced foo
's type yet, so the program is ill-formed.
As to why you are allowed to capture something before its initialization, see generally Why is 'int i = i;' legal?. We have many examples of recursive lambdas using std::function
:
std::function<void(int)> foo = [&foo](int i){ return foo(i - 1); };