The Artima Developer Community
Sponsored Link

Java Buzz Forum
Closures Comes To C++0x

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Weiqi Gao

Posts: 1808
Nickname: weiqigao
Registered: Jun, 2003

Weiqi Gao is a Java programmer.
Closures Comes To C++0x Posted: Mar 30, 2008 6:33 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Weiqi Gao.
Original Post: Closures Comes To C++0x
Feed Title: Weiqi Gao's Weblog
Feed URL: http://www.weiqigao.com/blog/rss.xml
Feed Description: Sharing My Experience...
Latest Java Buzz Posts
Latest Java Buzz Posts by Weiqi Gao
Latest Posts From Weiqi Gao's Weblog

Advertisement
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 class 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.

Read: Closures Comes To C++0x

Topic: US Non-Immigrant Visa Application Rejects Firefox Users Previous Topic   Next Topic Topic: Eclipse out thinks MS Vista

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use