C++0x: Usage of lambda functions and Intel C++ compiler

What are lambdas? Lambdas are small functions that are defined on the fly, instead of being declared before their use. They are really more usable than functors which accomplish a similar task.

First thing I noticed in Eclipse is that lambdas are not supported by CDT. And no clear plan on when they will be, a not so good point for one of the most used IDE on Linux (I didn’t test if Visual Studio supported lambdas). It means that the indentation will be messy, the diagnostic messages will be wrong…

Now, for the actual use of lambdas, it’s almost easy. The syntax may not be the most obvious one, but it works. The trick is to know what to do with variables referenced inside the functor but that were not passed when calling it. Typically, the value to which you compare elements of a list. These values can be passed by reference or value, depending on the declaration of the lambda fucntion.

container.remove_if([&](Container::iterator::value_type nodeit)
       { return nodeit->expired();});

Here, the predicate is a lambda function whose external values are passed by reference (the & inside the declaration hooks), and there is only one argument, nodeit. It’s obvious that it is clearer (when used to the specific syntax) than having to declare a class with a constructor, members…

I’ve also noticed that Intel C++ compiler had trouble (in its 11.1 version) with complex functions that call lambdas, resulting in an internal error. I didn’t check with the latest version (12) because I still have to use the 11.1 version for a while. I hope they fixed the issue when they improved their C++0x compliance.

Lambdas are really neat, but their support in IDE and compilers need to be enhanced.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.