title: Generic member function pointer
body: <p>I'm wondering if there's  a way in standard C++ (it seems this is not supported but perhaps I didn't look hard) to declare a pointer to any class' member function with the same signature. For example, X and Y have echoX and echoY methods with the same signature</p>
  
  <pre><code>class X{
    int val;
  public:
   int echoX(int v)  {
   val = v;
   return v; }
     int getValue() const { return val; }
  };
  
  class Y{
  
     int val;
     public:
     int echoY(int v)  {
     val = v;
     return v;
     }
     int getValue() const { return val; }
  };
  </code></pre>
  
  <p>Some C++ implementations allow this functionality via extensions (e.g VCL makes use of the <code>__closure</code> keyword).</p>
  
  <pre><code>typedef int (__closure *IntFunPtr)(int);
  </code></pre>
  
  <p>Now, it's trivial to write a function that is able to call either <code>X::echoX</code> or <code>Y::echoY</code></p>
  
  <pre><code>void CallObjectMethod(IntFunPtr fPtr, int val){
  
      fPtr(val);//this can call any member method that accepts an int and returns an int
  }
  
  
  X x, x1;
  CallObjectMethod(&amp;x.echoX,4);
  CallObjectMethod(&amp;x1.echoX,20);
  
  Y y, y1;
  CallObjectMethod(&amp;y.echoY,10);
  CallObjectMethod(&amp;y1.echoY,15);
  </code></pre>
  
  <p>This functionality can be useful for implementing event handlers, among other things.</p>
  
  <p>Thank you</p>