← Back to context

Comment by kccqzy

2 days ago

C++ solves this problem by simply not allowing a nested function (lambda) to be converted to a function pointer, and thereby avoids this problem of trampolines and executable stack altogether. I think that’s a better design.

C++ has the same solution as I propose here: A wide function pointer type.

In C++ it is called std::function, but this comes with a bit of baggage. C++ 26 has std::function_ref which would be the exact equivalent to my wide pointer.

https://godbolt.org/z/GaP9jb5rE

A C++ lambda that doesn't close over anything can be converted to a function pointer: https://eel.is/c++draft/expr.prim.lambda#closure-12 This feature does turn out to be useful if you need to pun a C++ interface into a function pointer for a C ABI function.

  • I know that but it’s not relevant to the discussion. A nested function in C (or rather GCC-flavored C) that doesn’t close over anything doesn’t need a trampoline anyways.