And hopefully it does. C++ is due for a good shot in the arm, something to get people excited about. Working every day in Java as I do, and yearning for my C++ days, there are a few features in Java that would be exciting to have in C++. Not many, but there are a few :).
And one of them appears to be ready to be included in the standard, lambda expressions. Now Java doesn't have pure lambda expressions, but the inner class support comes close. And with C++0x support for more general lambda expressions, I think we have a winner on our hands. Here's an example:
int x;
calculateWithCallback([&x](int y) { x = y; });
This ain't your father's C++. To explain what's happening, we're passing an anonymous function that takes a parameter y, and we pass along with it a closure which passes on some of the context with the function, in this case a reference to x. Later on the calculateWithCallback function does something and then calls our function with a parameter value for y. We then execute and assign the value to our x and return.
Anyway, very cool. Callbacks is a very popular design pattern and we use it all the time when programming Eclipse plug-ins. Being able to do something like this in a concise manner in C++ will be very useful and help bring C++ into a new decade, or whenever they get the standard ratified.
[&x] ???
ReplyDeleteThis is the first time I've seen lambdas where you had to declare the variables that are in the closure. The closure is normally implicit.
Knowing C++ I bet there is some crazy ambiguity in they syntax that requires it to be this way. No other language with lambdas that I know of requires this.
It's not required. You could also just say [&]. I was just trying to be explicit. But yes, the compiler will have the power to figure out what you mean given the context.
ReplyDeleteAlso, if you use [] instead, it'll pass x by value and that's not what you would intend in this example, but could be done.
More details on the wikipedia page.
To do lambdas, don't you need garbage collection? Otherwise, what happens when the value goes out of scope?
ReplyDeletelambda func()
{
int x = 3;
return [&x](int y) { x = y; }
}
That's bad news isn't it?
Yes, that's covered. Apparently the behavior is 'undefined', i.e., kaboom. So you have to use it carefully. And hopefully the compiler will notice, although I'm sure that would be limited.
ReplyDeleteI've heard members of the C++ standards committee mention the "0x" suffix could be a hex value, which could give them a few more years to sort it out.
ReplyDeleteHow about C++0B?
LOL. I can believe that.
ReplyDeletegoing back to bronson... even though the standard may make it undefined, in most c++ implementation that callback function will just write directly to the stack. You can get into similar problems without any fancy c++:
ReplyDeleteint * foo () {
int x=3;
return &x;
}
doug said:
ReplyDelete"Also, if you use [] instead, it'll pass x by value and that's not what you would intend in this example, but could be done."
Nope, using [] would give a compile error while [=] would pass x by value, right?
Good catch. I saw both on the wiki. I was assuming that [] was the short hand for [=] as opposed to [&]. I'd need to look at the spec to make sure.
ReplyDelete