Great answer ! Thank you very much, it answered almost everything !
But what about, in the exemple you gave me (which is great by the way) if foo as parameters ? Those parameters are passed on the stack by copy to the function, and then, copied to the heap (resulting in two copies) ?
Le 21/09/2011 19:56, Simen Kjaeraas a écrit :
void foo() { int x = 5; auto dg = () {x = 4;} dg(); } is roughly equivalent to: typedef struct foo_dg_1_delegate { void (*funcptr)(struct foo_dg_1_context*); void* ptr; }; typedef struct foo_dg_1_context { int x; }; void foo_dg_1(struct foo_dg_1_context* ctx) { ctx->x = 4; } void foo(void) { struct foo_dg_1_delegate dg; struct foo_dg_1_context* ctx = (struct foo_dg_1_context*)malloc(sizeof(struct foo_dg_1_context)); dg.funcptr = &foo_dg_1; dg.ptr = ctx; ctx->x = 5; dg.funcptr(dg.ptr); }