C++0x cool features: Lambda function

Have you heard that a new c++ draft is coming soon? It’s called c++0x for the moment and includes some new cool features.
A good description of the new features.


I’ve been recently playing around with lambda function support and looks pretty stable in the gcc compiler. I haven’t tried but I read that VC2010 has lambda support as well.

It gives support for in-place anonymous function definitions with context access, which basically helps us in a lot of situations, for example, if a given function, say wait_5minutes_call(callback_function), requires a callback we could easily do:

wait_5minutes_call( [ ](){ std::cout << "Callback called\n";} );

And thats all!! cool isn’t it?
A more complete example:

#include <iostream>
#include <functional>
#include <algorithm>
void print_function(int size, std::function funct){
    std::cout << funct(size) << std::endl;
int main(int argc, char* argv[])
    auto myfnc = [](int size){return size*size;};
    return 0;

The syntax is:

[howtogetcontext](ptype parameter){body} // implicit return type resolution
[howtogetcontext](ptype parameter) ->returntype {body} // explicit return type resolution

howtogetcontext specifies how we get the context, it can be [&] by ref or [=] by value(a copy), or we can say [&,variablename] for get everything by ref except variablename.

What is the context?

The context are the variables scoped in the place we define the lambda function.
Yes, we can access those variables even if we pass the lambda function far away of the current scope definition. Of course if any of those variables went out-of-scope or was freed and we refer to them by reference we’ll get a big crash, there’s no magic :).
We can access everything that is in scope on definition, locals, even this pointer if the function was defined inside an object method, actually, any method defined inside an class method is a friend of the class.

Ok, how do I get those cool lambda functions?

We need to download the gcc development branch for lambda, as the trunk doesn’t have lambda support.

svn co svn://gcc.gnu.org/svn/gcc/branches/cxx0x-lambdas-branch gcc-lambda
mkdir build
cd build
# Compilation can take several hours, depends on your machine
../gcc-lambda/configure --program-prefix=lambda --disable-libgcj --enable-languages=c++ --disable-werror
make -j3
make install

Now you have a lambdag++ binary in your system which you can use to compile c++ code with lambdas just like this:

lambdag++ --std=g++c0x main.cpp -o main

Don’t forget the –std=c++0x

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: