2

Possible Duplicate:
Parentheses at the end of a C++11 lambda expression

#include <iostream>

int main(int argc, char* argv[])
{
    int j;
    [&](){j = 10;}(); // why I need the last rounded parentheses () and what is their purpose ?
    // ...
    return(0);
}

I get almost everything about how the lambda works, my last question is about why i need the last couple of parentheses like reported in the above code.

The blueprint for a lambda is

[](){}

Also, I'm taking an input for my lambda by reference here, I'm directly writing into j with this lambda, but my compiler complains about the fact that this lambda generates an unused value if i don't put the extra () at the end of the lambda.

So, in the end, a lambda is this

[](){}

or this

[](){}()

?

Community
  • 1
  • 1
axis
  • 874
  • 2
  • 7
  • 13

3 Answers3

5

The [](){} defines a temporary lambda functor, the final () invokes its operator() (i.e. the function call operator) => you are defining a (temporary) lambda and calling it on the spot.

You may "see" it better as

([](){})()
 ^^^^^^ ^^ 
   ||    invokes the "function call operator"
 lambda definition
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
4

A lambda function is [](){}. When you add the parenthesis after it, it exectutes the lambda function.

Writing [](){}, you simply declare the function. This gives you the ability to store it with something like auto my_lambda = [](){}, for a later call my_lambda().

tomahh
  • 13,441
  • 3
  • 49
  • 70
  • as before, http://stackoverflow.com/questions/13110413/using-values-from-lambdas-in-c-11#comment17822360_13110419 , i don't really get this behaviour ... – axis Oct 28 '12 at 16:01
  • so it's never implicit the fact that that lambda needs to be executed/used. – axis Oct 28 '12 at 16:06
3

The call operator will immediately-execute the lambda.

int x = [] { return 5; }(); // x == 5
David G
  • 94,763
  • 41
  • 167
  • 253
  • ok, now, why i need to do this ? Lambda functions are not maded to be directly used in the code ? it's not implicit the fact that i want to use lambda where i write them ? – axis Oct 28 '12 at 16:00
  • 1
    @axis You can store a lambda function and use it whenever you like. You can even pass it to other functions. – Joseph Mansfield Oct 28 '12 at 16:02
  • @axis: the *point* of lambdas is exactly to define functors "on the fly". – Matteo Italia Oct 28 '12 at 16:06
  • 1
    @axis Lambdas are like functors, but not exactly the same. See this answer -- http://stackoverflow.com/a/7627218/701092 – David G Oct 28 '12 at 16:07
  • @MatteoItalia yes, but i mean the assignment for later use works just like for the functors ? – axis Oct 28 '12 at 16:08
  • 3
    @axis Yes. As the standard says: "A closure object behaves like a function object." Or technically, "The closure type for a lambda-expression has a public inline function call operator (13.5.4) whose parameters and return type are described by the lambda-expression’s parameter-declaration-clause and trailing-return-type respectively." – Joseph Mansfield Oct 28 '12 at 16:09
  • ok, perfect, now i got this, thanks everyone. – axis Oct 28 '12 at 16:11