Here is a simple example to start with ...
void Example(void (*x)(void))
{
x();
}
and the challenge will be ...
Example([] { cout << "do something\n"; });
This is very similar to your C # example. And there are better, more universal ways to do this, as the comments show. If you wanted to return a value and take a parameter, you could do something like this ...
int Example2(int (*y)(int i), int p)
{
return y(p);
}
auto ret = Example2([](int p) -> int { cout << p << "\n"; return p; }, 2);
It will be similar to the C # version as follows
int Example2(Func<int,int> y, int p)
{
return y(p);
}
var ret = Example2((p) => { return p; }, 2);
source
share