Re:How to design a replacement for C++
by bhiggins on Wednesday July 21, 2010 @09:06PM
(#1537038)
I figure closures would be hard to do in a programmer-friendly way without GC.
[ Parent
| on HN ]
The C++ closure library we use internally at Google has you specify whether the callback is permanent or not. If it isn't permanent, its deleted right after executing. It is a little painful maintaining the memory of your arguments, but people have generally converged to packing them all into a heap allocated struct and deleting when appropriate.
Re:How to design a replacement for C++
by mathias_10gen on Wednesday July 21, 2010 @10:39PM
(#1537200)
C++0x managed to add closures without adding GC. The only issue is that if you want to the closure to outlive the current stack frame you have to capture by value ([=] rather than [&]) or use a shared_ptr (which is then copied by value).
If you implement closures as thunks, you can just free them when done, no need for GC.
closure int foo(int x) {return x + y;}
int(*foo_p)(int) = malloc(sizeof(foo));
memcpy(foo_p,&foo,sizeof(foo));
return foo_p;
So, you can add working closures in one keyword (and possibly a special case for function pointer decay).
Re:How to design a replacement for C++
by albertzeyer on Wednesday July 21, 2010 @09:32PM
(#1537088)
smart pointers would also do it.