C++ member function wrapper for interfacing with C APIs Some C API interfaces use callbacks. Often these callbacks have a user-defined parameter of type void * which can be used as a class instance pointer in C++. Usually in order to call the member function you need to write a static class function which would cast the void * parameter to an instance pointer and call the member function. I wanted to write a wrapper that would instantiate a static function which takes void * as a parameter and the rest of arguments is exactly like in the member function passed to wrapper.

template&lt;
    typename Result,
    typename Function,
    auto Func
&gt;
struct instance_call_helper;

template&lt;
    typename Result,
    typename Base,
    typename... Args,
    auto Func
&gt;
struct instance_call_helper&lt;
    Result,
    Result(Base::*)(Args...),
    Func
&gt; {
    static Result call(void * ref, Args ... args) {
        Base * instance = reinterpret_cast&lt;Base *&gt;(ref);
        return (instance-&gt;*Func)(std::forward&lt;Args&gt;(args)...);
    }
};

template &lt;
    typename Base
&gt; struct instance_type_helper;
template &lt;
    typename Result, typename Base, typename... Args
&gt;
struct instance_type_helper&lt;Result(Base::*)(Args...)&gt; {
    using result_t = Result;
};

template&lt;auto Func&gt;
#if __cpp_concepts &gt;= 201507
requires std::is_member_function_pointer&lt;decltype(Func)&gt;::value
#endif
class instance_call {
    using functor_t = decltype(Func);
    using type_helper = instance_type_helper&lt;functor_t&gt;;

public:
    using helper = instance_call_helper&lt;
        typename type_helper::result_t,
        functor_t,
        Func
    &gt;;
};


Intended usage:

class Bar {
private:
    int m_a1;

public:
    Bar(int a1):
        m_a1(a1) {}

    int baz(int a2) {
        return m_a1 + a2;
    }
};

int foo() {
    Bar b { 3 };
    // Pointer we are going to pass somewhere as a callback
    // f has signature int (*)(void*, int)
    auto * f = &amp;instance_call&lt;&amp;Bar::baz&gt;::helper::call;
    // Intended way of calling it — returns 5
    return f(&amp;b, 2);
}
After using the code for a while I noticed the following changes could be used:


I don't need instance_type_helper class. Result type will be taken out of the function type. Thus, instance_call_helper turns into following:

template&lt;
    typename Function,
    Function F
&gt;
struct instance_call_helper;

template&lt;
    typename Result,
    typename Base,
    typename... Args,
    Result(Base::* Func)(Args...)
&gt;
struct instance_call_helper&lt;
    Result(Base::*)(Args...),
    Func
&gt;

In order to use this code with GCC 6, one can check the value of __cpp_deduction_guides macro:

#if __cpp_deduction_guides &gt;= 201606
template&lt;
    auto F
&gt;
#else
template&lt;
    typename Function,
    Function F
&gt;
#endif

For use with C code (it's not portable, as noticed in the comments), some more advanced typechecking can be used for arguments, like std::is_fundamental or std::is_pod