title: minimum instantiate class object, when query getInstance<T>(), based on inheritance
body: <p>User of my library will ask for an instance of any class via <code>getInstance&lt;T&gt;()</code>.      </p>
  
  <p>Among all classes <code>{T1,T2,T3,...,Tn}</code> that user asks (appearance of calling <code>getInstance&lt;T&gt;()</code>) in a certain program, if there is a pair of <code>(i,j)</code> and <code>Ti</code> derived from <code>Tj</code>, I will create only the bigger one, e.g. <code>Ti</code>.</p>
  
  <p>Here is an example.<br>
  Assume that user's code code ask me to create these classes:-      </p>
  
  <ul>
  <li>Physic         </li>
  <li>Rigid</li>
  <li>Constraint</li>
  <li><p>Graphic</p>
  
  <p>and their inheritance is as followed:-</p></li>
  </ul>
  
  <p><em>(The above four classes are marked with yellow in the diagram.)</em></p>
  
  <p><a href="https://i.stack.imgur.com/KnYZv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KnYZv.png" alt="enter image description here"></a></p>
  
  <p>My code will create <strong>minimum</strong> amount of class instances that cover the whole tree.<br>
  In this case, the best solution are <strong>3</strong> : <code>Rigid</code>, <code>Constraint</code>, <code>Graphic</code>.<br>
  Roughly speaking, the three arrows <em>pass</em> all classes in the tree.</p>
  
  <p><a href="https://i.stack.imgur.com/siOqA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/siOqA.png" alt="enter image description here"></a></p>
  
  <p>In this case, there will be 2 instances of <code>Physic</code>, but I don't care.  (Thank Christian Hackl)     </p>
  
  <p>More specifically, <code>getInstance&lt;Physic&gt;()</code> can return any of them.<br>
  The underlying type can be <code>Rigid*</code> or <code>Constraint*</code> - both are OK.</p>
  
  <p>If there are many <strong>minimum</strong> solutions, any one of them are OK.</p>
  
  <p>Here is what I dream :-</p>
  
  <pre><code>//..... some list to cache std::function .... ?
  template&lt;class T&gt; T* getInstance(){
      //return new T()  ? 
  }
  class B{};
  class C{};
  class D : public B{};
  int main(){
      //..... allow to insert some code here ...... (a little less preferable)
      B* b=getInstance&lt;B&gt;();   //return B*  (implicitly cast D*-&gt;B*)
      C* c=getInstance&lt;C&gt;();   //return C*
      D* d=getInstance&lt;D&gt;();   //return D* 
      assert( static_cast&lt;D*&gt;(b) == d );
      delete d;    delete c;
      //no memory leak
  }
  </code></pre>
  
  <p>How to do it?<br>
  This is closest question : <a href="https://stackoverflow.com/questions/39075804">Create &quot;custom typeid&quot; for many types AND cache their sizeof()</a></p>
  
  <p>I feel that it is possible : I have to cache the type by using :-</p>
  
  <ul>
  <li>lambda without capture / <code>std::function</code> -> keep it in a list  </li>
  <li>The cache function call <code>std::is_base_of</code>.   </li>
  <li>Then, I have to recognize all leaf-nodes in some ways.      </li>
  <li>Amount of classes that have to be instantiated = amount of leaf-nodes.     </li>
  </ul>
  
  <p>However, it is far from a concrete idea.  </p>
  
  <p>I want to create a very-sophisticated singleton manager for my libraries + educational purpose.<br>
  I know it is quite not a good practice,  but I believe the solution will enlighten me in C++ area.<br>
  <sub> Just in case it is not so obvious - this is not a homework / interview.     </sub></p>