Why I love C++11

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
{[](int x, int y)->int { return x*y; }};
Is this called Lambda expression or anonymous function?
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
Topic archived. No new replies allowed.