I read this question here on SO and stumbled over the best voted answer, which used code like this to call a lambda recursively:
std::function
Let's simplify the declaration a bit by using simpler types and expressions. We'll use int
instead of std::function
, 42
instead of the lambda, and f += 1
instead of f(3)
:
int f{42}, dummy((f += 1, 0));
To make it even more obvious, we can also use braces instead of parentheses for the second initialisation:
int f{42}, dummy{(f += 1, 0)};
This way, it should be clearer. It's a declaration which declares two variables: f
and dummy
. f
is initialised with 42
, and dummy
is initialised with this expression: (f += 1, 0)
. That one's using the comma operator to first evaluate f += 1
, discard the result, and then use the value 0
to initalise dummy
.
Going back to the full (nonsimplified) declaration:
The type of both variables f
and dummy
is std::function
. First f
is initialised with a lambda. Then, dummy
is initialised using a comma expression. The left-hand side of that expression, f(3)
, is evaluated and forgotten. The right-hand side, nullptr
, is then used to initialise dummy
. Initialising a std::function
with nullptr
results in creating an empty std::function
object (the same as a default-constructed one).
The whole purpose of dummy
is to introduce some extra context on the same line (= in the same declaration) in which f
could be invoked.