title: C++11 and after: How to store an object pointer and method pointer, received in a templatized method, for later use?
body: <p><strong>Question: I have a template function that takes an object pointer of the template param class, and a method pointer to a method of that class.  I can then call that method on that object immediately.  But I don't <em>want</em> to call it immediately.  Instead, I want to save both pointers for future use, and call them at a later time in code that won't be contextually aware of what that type is.</strong></p>
  <p>In legacy C/C++99 code, we pass in a function pointer and a <code>void*</code> user data pointer to code that will do a callback (e.g., on a timer finishing, a user event, etc.)  We'd almost invariably pass in an object pointer as the user-data, and write a one-line C function that casts the user data pointer to that type and called a method on the object:</p>
  <pre><code>void TimerCB( void* pvUserData, void* pvCallerData ) {
      ( (Foo*) pvUserData )-&gt;TimerDone( pvCallerData );
  }
  </code></pre>
  <p>In C++11, <code>std::function</code> lets us pass in lambdas and <code>std::bind</code>, or a C function without a user data.</p>
  <p>However, in practice, nearly every time I simply want to have a method on the current object called.  I can do that with a lambda or bind but it's verbose:</p>
  <pre><code>  class Timer {
  
        :
  
        virtual void SubscribeTimer( const char* pszTime,
                                     std::function&lt;void(Data*)&gt; pfn );
    };
  
    void Timer::SubscribeTimer( const char* pszTime,
                               std::function&lt;void(Data*)&gt; pfn ) {
        cout &lt;&lt; &quot;  calling std::function\n&quot;;
        Data d;
        pfn( &amp;d );
    }
  
    // Inside methods of class Foo:
  
    SubscribeTimer( &quot;14:59:50&quot;, std::bind( &amp;Foo::TimerDone, this, std::placeholders::_1 ) );
  
    SubscribeTimer( &quot;14:59:50&quot;, [this](Data* pdata){this-&gt;TimerDone( pdata );} );
  </code></pre>
  <p>I'm able to pass in method pointers, <em>if I know the class of their object at compile time</em>, like this:</p>
  <pre><code>  class Timer {
  
        :
  
        virtual void SubscribeTimer( const char* pszTime,
                                     void (Foo::*pfn)( Data* pd ), Foo* pfoo );
    };
  
    void Timer::SubscribeTimer( const char* pszTime, void (Foo::*pfn)( Data* pd ), Foo* pfoo ) {
        cout &lt;&lt; &quot;  calling method\n&quot;;
        Data d;
        (pfoo-&gt;*pfn)( &amp;d );
   }
  
    // Inside methods of class Foo:
  
    SubscribeTimer( &quot;14:59:50&quot;, &amp;Foo::TimerDone, this );
  </code></pre>
  <p>However, this is not acceptable, because my Timer class is of the utility library level of the project, and shouldn't need to be made aware of every possible user class like Foo.</p>
  <p>OK, so it turns out I can templatize that method so I no longer need to know what the type of object that Foo is or that the method is a method of.  This compiles without error.  (Method and class pointer swapped so it's clear which overloaded function is called.)</p>
  <pre><code>  class Timer {
  
        :
  
        template&lt;typename T&gt; void SubscribeTimer( const char* pszTime, T* pthis,
                                                  void (T::*pfn)( Data* pd ) );
    };
  
  
  
  template&lt;typename T&gt; void Foo::SubscribeTimer( const char* pszTime, T* pthis,
                                                  void (T::*pmethod)( Data* pd ) ) {
    cout &lt;&lt; &quot;  calling any method\n&quot;;
    Data d;
    (pthis-&gt;*pmethod)( &amp;d ); // &lt;-- PROBLEMATIC LINE
  }
  
  // Inside methods of class Foo:
  
  SubscribeTimer( &quot;14:59:50&quot;, this, &amp;Foo::TimerDone );
  </code></pre>
  <p>So... <strong>Victory!</strong>  That's the simpler syntax I wanted instead of the messier lambda and <code>std::bind</code> shown above.</p>
  <p><strong>BUT HERE IS MY QUESTION.</strong>  The above example works because the line labeled PROBLEMATIC LINE is in a context where the compiler knows the type of <code>pthis</code>.  But in practice, <code>SubscribeTimer()</code> doesn't call that callback right away.  Instead, it saves that value for future reference.  Long in the future, if the app is still running at 14:59:50, that callback will be called.</p>