C++Ox, Lambdas and Lots of Brackets
Friday, October 1, 2010 – 8:06 PMThe p&p dev team today had a random C++ moment (don’t worry it doesn’t happen often).
I’ve been playing around with C++, writing samples for “Parallel Programming with Microsoft Visual C++” and am a big fan of the new C++0x lambda syntax. It does however make heavy use of brackets. The following is legal code.
auto f = [](){};
Which defines an empty lamba, f, that has an empty capture list and takes no parameters.
There’s more on the C++Ox lambda syntax here. The bit I really like is being able to specify what get’s captured and how. For example the following captures r by reference and b by value:
int r = 0; int b = 2; auto f = [&r, b]() { r = b * b; } f();
In contrast C# take the opposite approach and captures everything by reference always. You can do this in C++ which does the same thing, everything captured by reference:
int r = 0; int b = 2; auto f = [&]() { r = b * b; } f();
Typically you’re better off being more specific about what you’re capturing and how. [&] is great for demos but I tend to avoid it in real code.
The samples for the C++ book make extensive use of lambdas because the Parallel Patterns Library does.
1 Trackback(s)
Sorry, comments for this entry are closed at this time.