Visual Studio 2010 and C++’0x
by HidekiAI on Jun.11, 2009, under Technology Opinions
Addendum (2009-10-30): If you haven’t watched this video, do so now. Microsoft has added 6th feature of C++ ’0x to VC++. In a nutshell, they had to add nullptr (the author calls it “null put-er” (so not to be confused with “null pointer”) and a new type nullptr_t. The part that gave me some warning signals were that Managed C++ (/CLR) already has nullptr as a keyword so if you wanted to have raw/unsafe pointer in your Managed C++ you’ll wonder how that would get affected, but watch the video, it’ll keep you in suspense!
As we already know, Microsoft is already preparing for Visual Studio 2010, which is supposed to support partial (if not most) of the C++ ’00. One of the best page to read when it comes to briefing yourself on what is to come is to read Bjarne Stroustrup‘s FAQ (from his point of views and opinions).
It seems that on 2010, additions made to VC is mainly to adopt towards templates, for those who’s been shying away from STL would probably not be excited, but evolution of C++ is now leaning towards more generalized utilities of common design patterns so that you wouldn’t have to reinvent the wheels (IMHO, that is what .NET is about as well).
One of the additions in which I am quite interested in that Microsoft has began support for is lambda. I think the best individuals who can explain what lambda is, are the ones who really know what they are doing, the ones who are involved in the design. So I want to first quote Stroustrup, then quote Microsoft. Combined together, you’ll get a clearer understanding of what it means.
A lambda expression is a mechanism for specifying a function object. The primary use for a lambda is to specify a simple action to be performed by some function. For example:
vector<int> v = {50, -10, 20, -30}; std::sort(v.begin(), v.end()); // the default sort // now v should be { -30, -10, 20, 50 } // sort by absolute value: std::sort(v.begin(), v.end(), [](int a, int b) { return abs(a)<abs(b); }); // now v should be { -10, 20, -30, 50 }The argument [&](int a, int b) { return abs(a)<abs(b); } is a “lambda” (or “lambda function” or “lambda expression”), which specifies an operation that given two integer arguments a and b returns the result of comparing their absolute values.
By the way, Microsoft calls the [] “lambda introducer”. Now Microsoft’s (both C++ and C# respectively):
Many programming languages support the concept of an anonymous function. A lambda expression is a programming technique that is related to anonymous functions. An anonymous function is a function that has a body, but does not have a name. A lambda expression implicitly defines a function object class and constructs a function object of that class type. You can think of a lambda expression as an anonymous function that maintains state and that can access the variables that are available to the enclosing scope.
… and the C# …
A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.
All lambda expressions use the lambda operator “>=“, which is read as “goes to”. The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x >= x * x is read “x goes to x times x.” This expression can be assigned to a delegate type as follows:
delegate int del(int i); del myDelegate = x => x * x; int j = myDelegate(5); //j = 25
I intentionally quoted Stroustrup’s syntax first before Microsoft’s because it felt to me that it gave better impression of understanding.
So what we get out of all this is that we can basically create an anonymous (nameless) functions which you can optionally pass parameters to it. It may differ from language to language, but the basic idea is that you can create a shorthand and use it in its local scope. But why go through all this if we can quickly as well, write a template<> based functions?
TODO: Talk about how commonly lambda is used in Java… I’m not a Java programmer so cannot make strong claims…
If you want to start preparing to support the new additions but are still on VC8 or VC9, you can start by integrating Boost.
Boost::lambda:
for_each(v.begin(), v.end(), cout << _1 << ' ');
The same example, based on Visual Studio 2010 can be written…
Visual Studio 2010 lambda:
for_each(v.begin(), v.end(), [](int n) { cout << n << " "; });
I guess I could have used “_1” instead of “n” for the parameter but I did not know if it would take it as a reserved word, so I left it as what Microsoft’s example used.
The actual code (in GCC) are to follow, but to make it more obvious, I’ve not added the “using namespace std;“:
#include <iostream> #include <boost/lambda/lambda.hpp> #include <vector> #include <boost/iostreams/pipeline.hpp> int main(int argc, char **argv) { std::vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); std::for_each(v.begin(), v.end(), std::cout << boost::lambda::_1 << ' '); return 0; }
which will print:
1 2 3
and exists the application.
TODO: Talk about practical usage of lambda, the examples from boost and microsoft are not too useful in real world applications…
LinkedIn profile
Recent Comments