title: Test if a subclass overrides a virtual function in a base class
body: <p>I'm looking for a way to check to see if a child class overrides a function on it's base class. Comparing member function pointers works perfectly fine if they aren't virtual, but if they <em>are</em> virtual it does not work. This sample code is essentially what I'm having trouble with.</p>
  
  <pre><code>class Base {
      public:
  
      virtual void vfoo(){ cout &lt;&lt; "foo"; }
      virtual void vbar(){ cout &lt;&lt; "bar"; }
      void foo(){ cout &lt;&lt; "foo"; }
      void bar(){ cout &lt;&lt; "bar"; }
  };
  
  class Child : public Base {
      public:
  
      void vfoo(){ cout &lt;&lt; "foo2"; }
      void foo(){ cout &lt;&lt; "foo2"; }
  };
  
  int main (){
      //non-virtual cases, these work correctly
      cout &lt;&lt; (&amp;Base::foo == &amp;Child::foo) &lt;&lt; endl; //outputs false (good)
      cout &lt;&lt; (&amp;Base::bar == &amp;Child::bar) &lt;&lt; endl; //outputs true  (good)
  
      //virtual cases, these do not work correctly
      cout &lt;&lt; (&amp;Base::vfoo == &amp;Child::vfoo) &lt;&lt; endl; //outputs true (BAD, child::vfoo and base::vfoo are DIFFERENT FUNCTIONS)
      cout &lt;&lt; (&amp;Base::vbar == &amp;Child::vbar) &lt;&lt; endl; //outputs true (good, child::vbar and base::vbar are the same)
  
      return 0;
  }
  </code></pre>
  
  <p>Logically there's no reason this shouldn't work, but the C++ spec says otherwise (comparing virtual functions addresses this way is implementation defined).</p>
  
  <p>On GCC, type punning &amp;Base::vfoo and &amp;Child::vfoo to int has them both be "1" (and vbar is "9"), which look to be vtable offsets. The following code appears to correctly get the function addresses out of the vtable, and correctly reports different addresses for Child::vfoo and Base::bfoo, and the same address for vbar</p>
  
  <pre><code>template&lt;typename A, typename B&gt;
  A force_cast(B in){
      union {
          A a;
          B b;
      } u;
      u.b = in;
      return u.a;
  };
  
  template&lt;typename T&gt;
  size_t get_vtable_function_address_o(T* obj, int vtable_offset){
      return *((size_t*)((*(char**)obj + vtable_offset-1)));
  };
  
  template&lt;typename T, typename F&gt;
  size_t get_vtable_function_address(T* obj, F function){
      return get_vtable_function_address_o(obj, force_cast&lt;size_t&gt;(function));
  };
  
  
  int main (){
      Base* a = new Base();
      Base* b = new Child();
  
      cout &lt;&lt; get_vtable_function_address(a, &amp;Base::vfoo) &lt;&lt; endl; 
      cout &lt;&lt; get_vtable_function_address(b, &amp;Base::vfoo) &lt;&lt; endl; 
  
      cout &lt;&lt; get_vtable_function_address(a, &amp;Base::vbar) &lt;&lt; endl; 
      cout &lt;&lt; get_vtable_function_address(b, &amp;Base::vbar) &lt;&lt; endl; 
  
      return 0;
  }
  </code></pre>
  
  <p>This works fine on GCC, though the fact that I have to subtract 1 from the vtable offset for it to work seems a bit weird. But it doesn't work on microsoft's compiler (punning &amp;Base::vfoo to a size_t returns some garbage instead of a virtual table offset) (some experimenting here suggests that the correct offsets here would be 0 for vfoo and 4 for vbar)</p>
  
  <p>I am WELL AWARE that this stuff is implementation-defined, but I am hoping there is a way to do this that at least works on a few common compilers (gcc, msvc, and clang) since vtables are pretty standard at this point (even if it requires compiler-specific code)?</p>
  
  <p>Is there any way to do this?</p>
  
  <p>Note 1: I only need this to work on single inheritance. I don't use multiple or virtual inheritance</p>
  
  <p>Note 2: Re-emphasizing that I do not need a callable function, I only need to test if a child class has overwritten a specific virtual function. If there's a way to do this without needing to dig into the vtables for stuff, then that would be preferred.</p>