Feb 16, 2013 at 2:32am UTC
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
struct Test
{
int (*fp)(int a, int b) {[](int x, int y)->int { return x*y; }};
};
int main()
{
Test t;
std::cout << (*(t.fp))(7, 1) << std::endl;
}
7
Last edited on Feb 16, 2013 at 2:40am UTC
Feb 16, 2013 at 2:39am UTC
{[](int x, int y)->int { return x*y; }};
Is this called Lambda expression or anonymous function?
Feb 16, 2013 at 2:40am UTC
The [](int x, int y)->int { return x*y; }
is the lambda expression/anonymous function, the outer {} is the initializer list.
By the way, this is also lambda:
[]{};
It's the bare minimum - it takes no arguments and returns nothing.
Last edited on Feb 16, 2013 at 2:42am UTC