20

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?

1
  • When you return a function , you can call it immediately Commented Jun 21, 2016 at 18:33

1 Answer 1

22

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I would add that decltype(f)(f) in this context is a way of writing std::forward<T>(f) since you can't actually name T.
@Brian which T are you talking about?
@Dean the type deduced for the auto placeholder.
@Brian std::forward<decltype(f)>(f) would express the intent better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.