Initializing Dependencies in c++

Since the approach I described for initializing dependencies in C++ does not work in Visual Studio, and probably other C++ implementations as well, Shankar Unni suggested a more portable approach.  Granted it uses a Macro, but I think this one is justified.

#define DEPENDENCY_INITIALIZATION \
static bool initialization_function(); \
static bool is_module_initialized = initialization_function();\
static bool initialization_function()

This is composed of:

  1. A forward declaration of the initialization function.
  2. A static variable that will be initialized at load time (prior to calling main)
  3. The start of the definition for the initialization function.

This is mean to be called like this:

DEPENDENCY_INITIALIZATION
{
supply<A,1>::configure(destroy);
supply<B,1>::configure();
return true;
}

Where A and B are the classes to be registered for later initialization.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.