title: Check if class of one object is derived from class of another object in C++
body: <p>If I got objects <code>a</code> and <code>b</code>, how do I determine if the class of <code>a</code> inherits from the class of <code>b</code>?</p>
  
  <hr>
  
  <p>Background: I got a C++ library for which I would like to write a Python bindings generator. That library provides a set of classes derived from a common base class. For the Python bindings, I need a list of functions for all classes. I got a list of methods through <code>nm -D myLibrary.so</code>, but that is missing methods of classes inherited e.g. in the style of</p>
  
  <pre class="lang-cpp prettyprint-override"><code>template&lt;class WrappedClass&gt;
  class Wrapper: public WrappedClass {
    public:
      // ...
  };
  
  typedef Wrapper&lt;ClassA&gt; ClassB;
  </code></pre>
  
  <p>. So I got all functions of classes like <code>ClassA</code> and would just like to know to which <code>ClassB</code> they  belong.</p>
  
  <p>I can get a list of names of all available classes from the library on runtime, and can get objects with those types via a factory function that accepts class names and gives objects of that type. So the last piece is to dynamically determine which classes like <code>ClassB</code> belong to which classes like <code>ClassA</code>. Therefore the question.</p>
  
  <p>PS: I could in principle write a C++ code generator that generates test code that is then compiled against the library to determine which classes inherit from which others. That would come to the high cost of not being able to compile my code along with the library but requiring a second compilation. Since I have nearly everything apart from this problem here to circumvent that, I hope very much that there is a different solution.</p>
  
  <hr>
  
  <p>PPS: I was told to use <code>decltype</code> in the comments. That does not work:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;type_traits&gt;
  
  struct A {};
  struct B : A {};
  
  int main() {
      A* a = new A();
      A* b = new B();
    std::cout &lt;&lt; std::boolalpha;
    std::cout &lt;&lt; "A, B: " &lt;&lt; std::is_base_of&lt;A,B&gt;::value &lt;&lt; std::endl;
    std::cout &lt;&lt; "a, b: " &lt;&lt; std::is_base_of&lt;decltype(a),decltype(b)&gt;::value &lt;&lt; std::endl;
    std::cout &lt;&lt; "*a, *b: " &lt;&lt; std::is_base_of&lt;decltype(*a),decltype(*b)&gt;::value &lt;&lt; std::endl;
    std::cout &lt;&lt; "a., B: " &lt;&lt; std::is_base_of&lt;decltype(a),B&gt;::value &lt;&lt; std::endl;
    std::cout &lt;&lt; "*a., B: " &lt;&lt; std::is_base_of&lt;decltype(*a),B&gt;::value &lt;&lt; std::endl;
    return 0;
  }
  }
  </code></pre>
  
  <p>yields</p>
  
  <pre><code>A, B: true
  a, b: false
  *a, *b: false
  a., B: false
  *a., B: false
  </code></pre>
  
  <hr>
  
  <p>PPPS: One answer suggested to use</p>
  
  <pre class="lang-cpp prettyprint-override"><code>std::is_base_of_v&lt;std::decay_t&lt;decltype(*a)&gt;, std::decay_t&lt;decltype(*b)&gt;&gt;
  </code></pre>
  
  <p>. I can't get it to work:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;type_traits&gt;
  
  struct A {};
  struct B : A {};
  
  int main() {
      A* a = new A();
      A* b = new B();
    std::cout &lt;&lt; std::boolalpha;
    std::cout &lt;&lt; "A, B: " &lt;&lt; std::is_base_of&lt;A,B&gt;::value &lt;&lt; std::endl;
    std::cout &lt;&lt; "B, A: " &lt;&lt; std::is_base_of&lt;B,A&gt;::value &lt;&lt; std::endl;
    std::cout &lt;&lt; "*a, *b: " &lt;&lt; std::is_base_of&lt;std::decay_t&lt;decltype(*a)&gt;, std::decay_t&lt;decltype(*b)&gt;&gt;::value &lt;&lt; std::endl;
    std::cout &lt;&lt; "*b, *a: " &lt;&lt; std::is_base_of&lt;std::decay_t&lt;decltype(*b)&gt;, std::decay_t&lt;decltype(*a)&gt;&gt;::value &lt;&lt; std::endl;
    return 0;
  }
  </code></pre>
  
  <p>yields:</p>
  
  <pre><code>A, B: true
  B, A: false
  *a, *b: true
  *b, *a: true
  </code></pre>