title: C++ Compile time list of subclasses of a class
body: <p>I was thinking about how to create a list of all class which derive from a template base class.</p>
  
  <p>First I want to have a template Base class:</p>
  
  <pre><code>template &lt;typename T&gt;
  class Base
  {
  public:
      Base() {};
      virtual ~Base() {};
  };
  </code></pre>
  
  <p>and a class which inherits from the base template class:</p>
  
  <pre><code>class Foo : public Base&lt;Foo &gt;
  {
  public:
      Foo () {};
      virtual ~Foo () {};
  };
  </code></pre>
  
  <p>There might be any number of other subclasses like <code>Foo</code>. The result should look like something:</p>
  
  <pre><code>std::vector&lt;std::string&gt; allTemplates = Base&lt;Base&gt;::getAllTemplateClasses();
  </code></pre>
  
  <p>My question is if its possible to create an list of all sub-classes at compile time? The Magic should happed in the Base class or with a really little effort in the child class. </p>
  
  <p>I was thinking in different directions before. First I thought it is possible to use <code>constexpr</code>. Like every child class needs a static function with the signature:</p>
  
  <pre><code>constexpr static std::string name() { "Foo";}
  </code></pre>
  
  <p>Or thought maybe it is possible with meta programming and create a compile time list like the Example of <a href="http://www.codeproject.com/Articles/31694/A-Compile-Time-Data-Structure-Using-Template-Meta" rel="nofollow noreferrer">A Compile Time Data Structure Using,Template-Meta</a>. The problem here is that I don't know the head of for the template creation.</p>
  
  <p>Next I was thinking about to use macros and build an enum struct like this way <a href="https://stackoverflow.com/a/14277706/3578196">Enum structs expanding</a>. So I can't find any solution for this problem I want to ask you if its even possible?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>To say it clear: I want to have a list of the child objects, without any need to create them.</p>