| ▲ | nesarkvechnep 11 hours ago | |||||||||||||
I'm thinking of using C++ for a personal project specifically for the lambdas and RAII. I have a case where I need to create a static templated lambda to be passed to C as a pointer. Such thing is impossible in Rust, which I considered at first. | ||||||||||||||
| ▲ | pornel 8 hours ago | parent | next [-] | |||||||||||||
Yeah, Rust closures that capture data are fat pointers { fn*, data* }, so you need an awkward dance to make them thin pointers for C.
It requires a userdata arg for the C function, since there's no allocation or executable-stack magic to give a unique function pointer to each data instance. OTOH it's zero-cost. The generic make_trampoline inlines code of the closure, so there's no extra indirection. | ||||||||||||||
| ||||||||||||||
| ▲ | queuebert 4 hours ago | parent | prev [-] | |||||||||||||
In Rust, could you instead use a templated struct wrapping a function pointer along with #[repr(C)]? | ||||||||||||||