Atom Atom feed

Closures Comes To C++0x

Hurb Sutter: For me, easily the biggest news of the meeting was that we voted lambda functions and closures into C++0x. I think this will make STL algorithms an order of magnitude more usable, and it will be a great boon to concurrent code where it's important to be able to conveniently pass around a piece of code like an object, to be invoked wherever the program sees fit (e.g., on a worker thread).

Here is the report.

Here is an example:

double min_salary = ....
....
double u_limit = 1.1 ∗ min_salary;
std::find_if(employees.begin(), employees.end(),
    [&](const employee& e) {
       return e.salary() >= min_salary && e.salary() < u_limit;
    });

Here are some of the finer points of the syntax and semantics:

  1. An lambda expression has the general form [...](...)...{...}
  2. The [...] portion serves as a lambda introducer, it also indicates which local variables, or this, are captured from the environment
  3. The optional (...)... portion declares the parameters, thrown exceptions and return types of the lambda expression
  4. The {...} portion contains the code of the lambda expression
  5. Lambda expressions evaluates to closure objects, which are function objects whose classes are implicitly defined and named. There's no duck typing here. The duck typing happens in the template system

The simplest lambda expression in C++ is thus

[]{ }
which captures no local variables, takes no parameters, and performs no actions when invoked.

The most useful lambda expression will be introduced by [&], which captures every free variable in the lambda expression by reference. The [=] introducer introduces a lambda expression that captures everything by value. The [] captures nothing.

The lambda introducer may also contain an explicit list of variable names or this, in which case they are captured by the lambda expression whether explicitly referenced by the lambda expression body or not. I haven't figured out how this could be used except that it may make local variables live longer.

In C++, the closure feature is more of a syntactic sugar for function objects.

One irony here is that while in the past, we have strived to turn for loops into for_each statements, closures will make those for_each statements look (mostly) just like the for loops.



Re: Closures Comes To C++0x

I haven't figured out how this could be used except that it may make local variables live longer.
That is _the_ reason for using closures in the first place over just plain anonymous functions. It creates both a communication channel (if you create more than one closure that holds both variables) and a way for the closure to hold state that persists across invocations. It allows generators without creating a class (explicitly anyway), for example. In fact, the main benefit is that many things that previously may have required a small helper class in C++ can now be defined in place where you'd otherwise instantiate the helper object and passed around anonymously, so it helps both with data hiding and locality. It's good they are adding it to C++, but the syntax still sucks, though...

Re: Closures Comes To C++0x

You may have misunderstood my question. I'm talking about the captured variables that are not mentioned in the lambda body.

Re: Closures Comes To C++0x

While I know virtually nothing about this closure proposal, I can't see how it could be capturing variables that are not being named in the lambda expression, or rather, how you could observe from code that they are being captured. I would be very surprised if C++ mandated something unnecessarily slow/big in implementations.

Re: Closures Comes To C++0x

The interesting thing I noticed is with the 'return' statement. It looks like 'return' will return from the closure to the invoker of the closure. This is FCM style in the FCM vs BGGA discussion for Java :-)

Add a comment Send a TrackBack