Boost logo

Boost-Build :

From: David Abrahams (dave_at_[hidden])
Date: 2002-10-23 15:15:16

Alisdair Meredith <gclbb-jamboost_at_[hidden]> writes:

> Patrick Frants wrote:
> > We have found that msvc and gcc initialize all static objects within object
> > files that are linked directly. Object files within libraries get
> > initialized when a function within that object files is called.
> We have found the same problem with the borland compiler. It is double
> painful as typically we are registering creation functions with factory
> classes. The functionality of the cpp is only available through factory
> created object instances, so until the registration is run, nothing in
> the CPP is initialised, so our classes never register, and we get a
> lesson in circular logic :¬ (
> If anyone has a simple, widely applicable solution to this problem I am
> very interested!

Simple: don't rely on static initializers being run before the
standard says you can. You have to call a function in each translation
unit containing such registration functions at startup.

Sad but true.

One way to avoid some of the maintenance overhead of this approach is
to use explicit specializations of a function template:

template <int n> register_initializer() {}

template <int num_registrations>
void register_all_initializers()
register_initializer<num_registrations - 1>();
register_all_initializers<num_registrations - 1>();

template <> void register_all_initializers<0>() {}

Now you can explicitly instantiate specializations of
register_initializer in the translation units that need to have
initializers run.

David Abrahams
dave_at_[hidden] *
Building C/C++ Extensions for Python: Dec 9-11, Austin, TX

Boost-Build list run by bdawes at, david.abrahams at, gregod at, cpdaniel at, john at