Programming Techniques, Tools and Languages Group

Lambdas

GCC with lambdas

This page describes a partial implementation of the C++ standards proposal N2550: "Lambda Expressions and Closures," by Jaakko Järvi, John Freeman, and Lawrence Crowl. As of May 2008, there is a branch of GCC dedicated to developing support for C++0x lambda functions.

Example

The implementation currently supports the following features of the proposal:

int sum = 0 ;
std::for_each1 (begin, end, 2
  [&, f = factor4] 3 (int5 n) 6 { sum += n * f; }7 );
  1. Lambda expressions can be passed to function templates, and are usable with the std::CallableN class of concepts.
  2. A lambda function can be defined wherever a primary-expression is expected.
  3. Lambdas can "capture" variables from the surrounding scope, by reference or copy.
  4. Initializer expressions, an extension to the proposal, are supported.
  5. Argument types must be explicitly annotated.
  6. Return types are deducable and, thus, may be omitted.
  7. The body must be a compound-statement.
Additional features include:
  • When used inside a member function, a lambda
    • can capture this explicitly, or by default when it uses direct or indirect references to class members.
    • has private access to the class.

Bugs

  • Although they can appear within templates, lambda functions may not be used with dependent types.
  • There is currently an unexplained conflict with the flag -fno-elide-constructors.

Download

svn://gcc.gnu.org/svn/gcc/branches/cxx0x-lambdas-branch

Installation

Download the source. $ svn co svn://gcc.gnu.org/svn/gcc/branches/cxx0x-lambdas-branch gcc
$ ls
lambdas/
Make a build directory and change into it. $ mkdir build
$ cd build
Configure. Popular options include --prefix (for installation directory) and --enable-languages (to avoid building unused parts). $ ../gcc/configure --prefix="$PWD/../install" --enable-languages=c++
Build. This can be parallelized. $ make -j 4
Install. $ make install
Run. $ g++ -std=c++0x main.cpp

Upcoming Events




Share This Article




Headlines

About Us | Parasol Lab | Computer Science and Engineering | ©2010 Texas A&M University