C++ question. Help me!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1:
int m = [](int x)
{
	return [](int y)
		{
			return y*2;
		}(x)+3;
}(5);
cout<<m<<endl;

2:
auto ff=[](int x,int y)
{
	return x+y;
}(4,5);
cout<<ff(21,12)<<endl;

how does it run?
what does it mean?
please!!! help me!!!
Last edited on
Lambdas are a C++11 feature.
Here is some documentation on them: http://en.cppreference.com/w/cpp/language/lambda

You will need a compiler that supports C++11 in order to compile this.

Here is the code in action on a C++11 compiler: http://coliru.stacked-crooked.com/a/8f1285680f453398

As you can see there is an error in the second code, ff being of type int, which is not a callable type.

You might also be interested in what auto does:
http://en.cppreference.com/w/cpp/language/auto

You can just replace auto with int in this case, since the return type of the lambda is int.

The author's original intention might have been for ff to be a lambda. In that case, he should have omitted the call from line 15. That would make second code well-formed, as you can see here: http://coliru.stacked-crooked.com/a/bcefad00afebd000
Last edited on
thanks you!
Topic archived. No new replies allowed.