Static to Instance method trampolining with templates I finally have a working trampoline mechanism. Would anyone care to critique it?

Ideone

#include &lt;iostream&gt;
using namespace std;

#include &lt;map&gt;

template&lt;typename T&gt;
class Base
{
public:    
    typedef void (T::*PFunc)(void);
    typedef void (*PHandler)(void*);

    using method_map_t = map&lt; string, PHandler &gt;;
    static method_map_t&amp; methods( ) {
        static method_map_t* map_of_methods{};
        if( ! map_of_methods ) map_of_methods = new method_map_t;
        return *map_of_methods;
    }

    static void register_method( string name, PHandler handler ) {
        methods()[name] = handler;
    }

    // generic trampoline
    template&lt;PFunc sig&gt;
    static void Handler( void* pInstance ) {
        T* f = reinterpret_cast&lt;T*&gt;(pInstance);
        (f -&gt;* sig)();
    }
};


...

class Final : Base&lt;Final&gt;
{
public:
    void Foo(){cout&lt;&lt;"got foo";}
    void Bar(){cout&lt;&lt;"got bar";}

    static void init(){
        // register_method populates a table of "extern C" function pointers.
        register_method( "foo", static_cast&lt;PHandler&gt;( &amp;Handler&lt;&amp;Final::Foo&gt; ) );
        register_method( "bar", static_cast&lt;PHandler&gt;( &amp;Handler&lt;&amp;Final::Bar&gt; ) );
    }
};

void InvokeFromC(void* inst, string name) {
    Base&lt;Final&gt;::PHandler h = Base&lt;Final&gt;::methods()[name];
    (*h)(inst);
}

int main() {
    Final* f = new Final{};
    f-&gt;init();
    // simulate invoking from C 
    InvokeFromC( f, "foo" );

    // your code goes here
    return 0;
}


This question follows from here.
I will maintain my list of improvements in this answer. 

There is a more succinct way to expose the mechanism to the consumer:

class Final : Base&lt;Final&gt;
{
    register_method&lt;&amp;Final::Foo&gt;( "foo" ); //, static_cast&lt;PHandler&gt;( &amp;Handler&lt;&amp;Final::Foo&gt; ) );
    register_method&lt;&amp;Final::Bar&gt;( "bar" ); //, static_cast&lt;PHandler&gt;( &amp;Handler&lt;&amp;Final::Bar&gt; ) );
    :

class Base
{
    :
    template&lt;PFunc func&gt;
    static void register_method( string name ) {
        methods()[name] = &amp;Handler&lt;func&gt;;
    }
    :


This has extra advantage in that it avoids the following nasty issue:


  Note: (Eelis on IRC) gcc is known not to be able to resolve addresses
  of function template specializations passed to functions. You can work
  around it either by declaring separate variables for the function
  pointers, or using a dirty cast (or a nice cast like
  boost::implicit_cast)


It doubles the number of templated functions getting generated: now 2 are generated for each method instead of one. But at a small price, it is much cleaner to the consumer!

Also function pointer typedef-s could be rewritten:

//typedef void (T::*PFunc)(void);
//typedef void (*PHandler)(void*);

using PFunc = void (T::*)(void);
using PHandler = void(*)(void);


http://ideone.com/ZFQb1f