title: Why might a static data member not get initialized?
body: <p>I'm trying to register a bunch of classes with a factory at load time.  My strategy is to harness static initialization to make sure that before main() begins, the factory is ready to go.  This strategy seems to work when I link my library dynamically, but not when I link statically; when I link statically, only some of my static data members get initialized.</p>
  
  <p>Let's say my factory builds Cars.  I have CarCreator classes that can instantiate a handful of cars, but not all.  I want the factory to collect all of these CarCreator classes so that code looking for a new Car can go to the factory without having to know who will be doing the actual construction.</p>
  
  <p>So I've got</p>
  
  <p>CarTypes.hpp</p>
  
  <pre><code>enum CarTypes
  {
     prius = 0,
     miata,
     hooptie,
     n_car_types
  };
  </code></pre>
  
  <p>MyFactory.hpp</p>
  
  <pre><code>class CarCreator
  {
  public:
     virtual Car * create_a_car( CarType ) = 0;
     virtual std::list&lt; CarTypes &gt; list_cars_I_create() = 0;
  };
  
  class MyFactory // makes cars
  {
  public:
     Car * create_car( CarType type );
     void factory_register( CarCreator * )
  
     static MyFactory * get_instance(); // singleton
  private:
     MyFactory();
  
     std::vector&lt; CarCreator * &gt; car_creator_map;
  };
  </code></pre>
  
  <p>MyFactory.cpp</p>
  
  <pre><code>MyFactory:: MyFactory() : car_creator_map( n_car_types );
  
  MyFactory * MyFactory::get_instance() {
     static MyFactory * instance( 0 ); /// Safe singleton
     if ( instance == 0 ) {
        instance = new MyFactory;
     }
     return instance;
  }
  
  void MyFactory::factory_register( CarCreator * creator )
  {
     std::list&lt; CarTypes &gt; types = creator-&gt;list_cars_I_create();
     for ( std::list&lt; CarTypes &gt;::const_iteator iter = types.begin();
           iter != types.end(); ++iter ) {
        car_creator_map[ *iter ] = creator;
     }
  }
  
  Car * MyFactory::create_car( CarType type ) 
  {
     if ( car_creator_map[ type ] == 0 ) { // SERIOUS ERROR!
        exit();
     }
     return car_creator_map[ type ]-&gt;create_a_car( type );
  }
  </code></pre>
  
  <p>...</p>
  
  <p>Then I'll have specific cars and specific car creators:</p>
  
  <p>Miata.cpp</p>
  
  <pre><code>class Miata : public Car {...};
  
  class MiataCreator : public CarCreator {
  public:
     virtual Car * create_a_car( CarType );
     virtual std::list&lt; CarTypes &gt; list_cars_I_create();
  private:
     static bool register_with_factory();
     static bool registered;
  };
  
  bool MiataCreator::register_with_factory()
  {
     MyFactory::get_instance()-&gt;factory_register( new MiataCreator );
     return true;
  }
  
  bool MiataCreator::registered( MiataCreator::register_with_factory() );
  </code></pre>
  
  <p>...</p>
  
  <p>To reiterate: dynamically linking my libraries, MiataCreator::registered will get initialized, statically linking my libraries, it will not get initialized.</p>
  
  <p>With a static build, when someone goes to the factory to request a Miata, the miata element of the <code>car_creator_map</code> will point to NULL and the program will exit.</p>
  
  <p>Is there anything special with private static integral data members that their initialization will be somehow skipped?  Are static data members only initialized if the class is used?  My CarCreator classes are not declared in any header file; they live entirely within the .cpp file.  Is it possible that the compiler is inlining the initialization function and somehow avoiding the call to MyFactory::<code>factory_register</code>?</p>
  
  <p>Is there a better solution to this registration problem?</p>
  
  <p>It is not an option to list iall of the CarCreators in a single function, register each one explicitly with the factory, and then to guarantee that the function is called.  In particular, I want to link several libraries together and define CarCreators in these separate libraries, but still use a singular factory to construct them.</p>
  
  <p>...</p>
  
  <p>Here are some responses I am anticipating but which do not address my problem:</p>
  
  <p>1) your singleton Factory isn't thread safe.
  a) Shouldn't matter, I'm working with only a single thread.</p>
  
  <p>2) your singleton Factory may be uninitialized when your CarCreators are being initialized (i.e. you've got a static initialization fiasco)
  a) I'm using a safe version of the singleton class by putting the singleton instance into a function.  If this were a problem, I should see output if I added a print statement to the <code>MiataCreator's::register_with_factory</code> method:  I don't.</p>