Boost logo

Boost-Build :

From: David Abrahams (dave_at_[hidden])
Date: 2002-10-24 11:57:10



Patrick Frants <gclbb-jamboost_at_[hidden]> writes:

> David Abrahams <dave_at_[hidden]> wrote in
> news:uk7k8ap4b.fsf_at_[hidden]:
>
> > Alisdair Meredith <gclbb-jamboost_at_[hidden]> writes:
> > 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.
>
> So I have register_initializer instantiations from 0 to n in all cpp
> files.

You only need them where you have static initializers that must be run
at startup.

> And the register_all_initializers template will recursively call these
> function from n down to 0. I see it is easy to extend this with an extra
> template parameter for the library

Hmm? I don't understand what you're proposing.

> and the initializers within a library count from 0 to n.

You can't get the templates to do the counting for you, unfortunately,
but you'll get a link error if you ever omit or duplicate one of the
numbers.

> Is there a chance that you would hit an artificial limit in the
> template expansion code of the compiler if n is large (> 1500)?

Yes, but your compiler vendor is required to document the
implementation limit for template instantiation depth.

-- 
David Abrahams
dave_at_[hidden] * http://www.boost-consulting.com

Building C/C++ Extensions for Python: Dec 9-11, Austin, TX
http://www.enthought.com/training/building_extensions.html

 

Boost-Build list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk