Recode C -> C++ trampoline function macros using templates I'm reworking PyCXX (C++ wrapper for Python).

I'm currently examining the following macro code, and wondering whether there is a way to write it using templates rather than macros.

If so, the question becomes: what is the best way to write it? (I personally favour a concise macro solution over an awkward template solution).

#define PYCXX_NOARGS_METHOD_NAME( NAME ) _callNoArgsMethod__##NAME
#define PYCXX_VARARGS_METHOD_NAME( NAME ) _callVarArgsMethod__##NAME
#define PYCXX_KEYWORDS_METHOD_NAME( NAME ) _callKeywordsMethod__##NAME

// - - -

#define PYCXX_DECLARE_METHOD( THIS_CLASS, METHOD_NAME, Ax, NAME_OF_THIS_STATIC_METHOD, ... ) \
    \
    static PyObject* NAME_OF_THIS_STATIC_METHOD( PyObject* _pyObj_caller, PyObject* _a, PyObject* _k ) \
    { \
        DBG_LINE( Ax &lt;&lt; #NAME_OF_THIS_STATIC_METHOD &lt;&lt; Ax ); \
        try \
        { \
            Py::PythonClassInstance* base_cxx_obj   = reinterpret_cast&lt; Py::PythonClassInstance* &gt;( _pyObj_caller ); \
            THIS_CLASS*              this_class_obj = reinterpret_cast&lt; THIS_CLASS* &gt;             ( base_cxx_obj-&gt;m_pycxx_object ); \
            Py::Object result{  (this_class_obj -&gt; METHOD_NAME)(__VA_ARGS__)  }; /* __VA_ARGS__ uses _a and _k (see below) */ \
            return Py::new_reference_to( result.ptr() ); \
        } \
        catch( Py::Exception &amp; ) \
        { \
            DBG_LINE( "! ! !  Exception in PYCXX_DECLARE_METHOD ! ! !" ); \
            return 0; \
        } \
    }

#define PYCXX_NOARGS_METHOD_DECL(   _class, name ) PYCXX_DECLARE_METHOD( _class, name, "  &amp;  "     , PYCXX_NOARGS_METHOD_NAME(   name ) )
#define PYCXX_VARARGS_METHOD_DECL(  _class, name ) PYCXX_DECLARE_METHOD( _class, name, "  &amp; &amp;  "   , PYCXX_VARARGS_METHOD_NAME(  name ), Py::Tuple{_a} )
#define PYCXX_KEYWORDS_METHOD_DECL( _class, name ) PYCXX_DECLARE_METHOD( _class, name, "  &amp; &amp; &amp;  " , PYCXX_KEYWORDS_METHOD_NAME( name ), Py::Tuple{_a}, _k?Py::Dict{_k}:Py::Dict{} )


... and

#define PYCXX_ADD_METHOD( py_name, method_name, docs, flags ) \
    add_method( \
               #py_name, \
               (PyCFunction)method_name, \
               flags, \
               docs \
               )

#define PYCXX_ADD_NOARGS_METHOD(   py_name, name, docs )  PYCXX_ADD_METHOD( py_name, PYCXX_NOARGS_METHOD_NAME(   name ), docs, METH_NOARGS )
#define PYCXX_ADD_VARARGS_METHOD(  py_name, name, docs )  PYCXX_ADD_METHOD( py_name, PYCXX_VARARGS_METHOD_NAME(  name ), docs, METH_VARARGS )
#define PYCXX_ADD_KEYWORDS_METHOD( py_name, name, docs )  PYCXX_ADD_METHOD( py_name, PYCXX_KEYWORDS_METHOD_NAME( name ), docs, METH_VARARGS | METH_KEYWORDS )


It is used in a class to expose certain methods to Python:

Py::Object new_style_class_func_noargs( void )
{
    std::cout &lt;&lt; "new_style_class_func_noargs Called."           &lt;&lt; std::endl &lt;&lt; std::endl;
    std::cout &lt;&lt; "value ref count " &lt;&lt; m_value.reference_count() &lt;&lt; std::endl &lt;&lt; std::endl;
    return Py::None();
}
PYCXX_NOARGS_METHOD_DECL( new_style_class, new_style_class_func_noargs )

static void init_type(void)
{
    :
    PYCXX_ADD_NOARGS_METHOD(    func_noargs , new_style_class_func_noargs,  "docs for func_noargs"  );
}


The reason for this architecture is that Python can only fit "extern C" style functions into its tables.  But this code is attempting to run C++ instance member functions.

Hence it has to trampoline off a static member function (as a static member function type casts to an "extern C" style function).

And we require a separate static member function for each method we wish to expose.

Is there any way to use templates to accomplish this?
Surprisingly (to me at any rate) there is a template solution.

It appears that it is possible to do:

template&lt;FuncSignature funcPointer&gt;
myFunc(){...}


And later:

x = myFunc&lt;&amp;MyClass::MyMethod&gt;


You can pick up the trail here: https://stackoverflow.com/questions/26934036/coding-static-to-instance-method-trampoline-function-with-templates