This topic explains in some detail about using Intel's implementation of C++ Lambda expressions. In order to use lambda expressions, you need to request c++0x with the command-line option -std or /Qstd:
On Windows* systems: /Qstd=c++0x
On Linux* systems: -std=c++0x
Lambda expressions are introduced in the code by [lambda-captureopt]. If the expression does not capture any local variables or references with automatic storage duration, lambda-captureopt can be empty, leaving [] as the introducer.
For example, the lambda expression
[](int x) {return x%3==0;}
is equivalent to the expression unique(), where unique is a secret identifier generated and defined by the compiler as
class unique {
public:
bool operator()(int x ) const {return x%3==0;}
};
The lambda-parameter-declaration follows the lambda-introducer. If the parameter list is empty, the parentheses can be omitted. For example, the following lambda expressions are equivalent.
[] {return rand();}
[]() {return rand();}
The body of a lambda expression is a compound statement. A return type T for a lambda expression can be specified by writing ->T after the parameter list.
For example, the following lambda expressions are equivalent:
[](int x) {return x%3==0;}
[](int x) -> bool {return x%3==0;}
[](int x) -> bool {if( x%3==0 ) return true; else return false;}
If the return type is not explicitly specified, the return type is void unless the body has the form {return expression;}. In such a case the return type is the type of the expression. Consider the following example:
[](int x) { if (x % 3 == 0) return true; else return false; }
This expression is an error because you can't return a boolean value from a function returning void.
A lambda expression may include an exception specification after the parameter list and before the return type specification. The parameter list is necessary if there is an exception specification.
The following lambda expressions specify that they do not throw exceptions:
[](int x) throw() -> bool {return x%3==0;}
[]() throw() {return rand();}
Copyright © 1996-2010, Intel Corporation. All rights reserved.