- Nested Functions
- Why Closures Are Hard
- Blocks
- C++0x Lambdas
- Should You Use Closures?
C++0x Lambdas
The next version of C++, dubbed C++0x because was due for release sometime in the 20002009 timeframe (demonstrating that every project involving C++ takes longer than expected), includes support for something called lambdas.
Unfortunately, lambdas are less exciting than they might appear at first. The specification says the following:
If one or more names in the effective capture set are preceded by &, the effect of invoking a closure object, or a copy, after the innermost block scope of the context of the lambda expression has been exited is undefined.
This means that they're expected to be implemented referring directly to objects on the stack, and they don't provide a mechanism for transparently copying these objects to the heap. Therefore, they have no equivalent to the __block storage qualifier, and so are roughly equivalent to GCC's nested function extension.
The one advantage of lambdas over nested functions is that C++ already has a calling convention that permits a hidden argument, and these functions will use that convention. Unfortunately, this also means that they'll fit into the C++ type system in a way that prevents you from using them in place of C function pointers, making them marginally less useful than nested functions. On the plus side, at least they don't require an executable stack.