前提:GNU C语法
实现:
1.定义宏
#define lambda(RType, Body) ({ RType __fn__ Body; __fn__; })
2.例子
#include <stdio.h>
#define lambda(RType, Body) ({ RType __fn__ Body; __fn__; })
void baz(void(*fn)(int),int x)
{
fn(x);
}
void foo(int x)
{
baz(lambda(void,(int x){printf("lamda:%d\n",x);}),x);
}
int main() {
foo(100);
return 0;
}