Hacker News new | ask | show | jobs
by gpderetta 615 days ago
Interesting problem. The typical solution in C++ to deal with type erasing function pointer types is to go through a trampoline function:

    struct X {};
    void use_x(X*);
    using F = void(void*);
    void bar(F* fn, void* y) { fn(y); }

    template<class T, auto fn>
    void trampoline(void*arg) { return fn(reinterpret_cast<T*>(arg)); }

    X x;
    bar(&trampoline<X, use_x>, &x);
In plain C there is no way to generate the trampoline at the point of use in the same way template instantiation works, but it can be generated by a macro at global scope.