I'm looking at the following (presumably C++14) piece of code
auto min_on = [](auto&& f) {
return [f=decltype(f)(f)](auto&& arg0, auto&&...args) {
// call your function here, using decltype(args)(args) to perfect forward
};
}
what is the weird assignment in the lambda capture list? I've never seen an assignment in a capture list
f=decltype(f)(f)
How does this work?
As stated above, we can have any number of arguments but only a single expression. The lambda operator cannot have any statements and it returns a function object that we can assign to any variable.
The assignment operator, denoted by the “=” symbol, is the operator that is used to assign values to variables in Python. The line x=1 takes the known value, 1, and assigns that value to the variable with name “x”.
A Python lambda function behaves like a normal function in regard to arguments. Therefore, a lambda parameter can be initialized with a default value: the parameter n takes the outer n as a default value. The Python lambda function could have been written as lambda x=n: print(x) and have the same result.
That's what's called a Generalized Lambda Capture, and yes, it's C++14.
Basically it allows you to create a new variable as part of the capture list.
Text from link:
In C++11, lambdas could not (easily) capture by move. In C++14, we have generalized lambda capture that solves not only that problem, but allows you to define arbitrary new local variables in the lambda object. For example:
auto u = make_unique<some_type>( some, parameters ); // a unique_ptr is move-only go.run( [ u=move(u) ] { do_something_with( u ); } ); //move the unique_ptr into the lambda
In the above example, we kept the name of the variable
u
the same inside the lambda. But we’re not limited to that… we can rename variables:go.run( [ u2=move(u) ] { do_something_with( u2 ); } ); // capture as "u2"
And we can add arbitrary new state to the lambda object, because each capture creates a new type-deduced local variable inside the lambda:
int x = 4; int z = [&r = x, y = x+1] { r += 2; // set x to 6; "R is for Renamed Ref" return y+2; // return 7 to initialize z }(); // invoke lambda
In your specific instance, you have a lambda that is returning a lambda. The nested lambda is capturing f
(which was only a parameter in the parent lambda) by using this new syntax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With