Skip to content

Instantly share code, notes, and snippets.

@iains
Created August 14, 2023 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iains/fc53cdfdf55c383d1d33ba1c2850052a to your computer and use it in GitHub Desktop.
Save iains/fc53cdfdf55c383d1d33ba1c2850052a to your computer and use it in GitHub Desktop.
using a lambda trampoline - to making a closure into a function ptr.
// Closure type that runs the original function with the supplied args.
auto __callable = [&] {
std::__invoke(std::forward<_Callable>(__f),
std::forward<_Args>(__args)...);
};
// Trampoline to call the actual fn; we will pass in the closure address.
void (*__oc_tramp)(void*)
= [] (void *ca) { (*static_cast<decltype(__callable)*>(ca))(); };
now you can call __oc_tramp ( std::addressof (__callable) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment