Best way to build a list of per type data?
<p>Suppose I have some per-class data: (AandB.h)</p>

<pre><code>class A
{
public:
   static Persister* getPersister();
}

class B
{
public:
   static Persister* getPersister();
}
</code></pre>

<p>... and lots and lots more classes. And I want to do something like:</p>

<pre><code>persistenceSystem::registerPersistableType( A::getPersister() );
persistenceSystem::registerPersistableType( B::getPersister() );
...
persistenceSystem::registerPersistableType( Z::getPersister() );
</code></pre>

<p>... for each class.</p>

<p>My question is: is there a way to automate building a list of per-type data so that I don't have to enumerate each type in a big chunk (as in the above example)?</p>

<p>For example, one way you might do this is: (AutoRegister.h)</p>

<pre><code>struct AutoRegisterBase
{
   virtual ~AutoRegisterBase() {}
   virtual void registerPersist() = 0;
   static AutoRegisterBase*&amp; getHead()
   {
      static AutoRegisterBase* head= NULL;
      return head;
   }

   AutoRegisterBase* next;
};

template &lt;typename T&gt;
struct AutoRegister : public AutoRegisterBase
{
   AutoRegister() { next = getHead(); getHead() = this; }

   virtual void registerPersist()
   {
       persistenceSystem::registerPersistableType( T::getPersister() );
   }
};
</code></pre>

<p>and use this as follows:  (AandB.cxx: )</p>

<pre><code>static AutoRegister&lt;A&gt; auto_a;
static AutoRegister&lt;B&gt; auto_b;
</code></pre>

<p>Now, after my program starts, I can safely do: (main.cxx)</p>

<pre><code>int main( int, char ** )
{
    AutoRegisterBase* p = getHead();
    while ( p )
    {
        p-&gt;registerPersist();
        p = p-&gt;next;
    }
    ...
}
</code></pre>

<p>to collect each piece of per-type data and register them all in a big list somewhere for devious later uses. </p>

<p>The problem with this approach is that requires me to add an AutoRegister object somewhere per type. (i.e. its not very automatic and is easy to forget to do). And what about template classes? What I'd really like is for the instantiation of a template class to somehow cause that class to get automatically registered in the list. If I could do this I would avoid having to have the user of the class (rather than the author) to remember to create a:</p>

<pre><code>static AutoRegister&lt; SomeClass&lt;X1&gt; &gt; auto_X1;
static AutoRegister&lt; SomeClass&lt;X2&gt; &gt; auto_X2;
...
etc....
</code></pre>

<p>for each template class instantiation.</p>

<p>For FIW, I suspect there's no solution to this.</p>
