|
Boost : |
Subject: Re: [boost] Is there any interest in static constructors?
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2013-02-05 08:33:46
On 04/02/13 18:51, Alexander Stoyan wrote:
>>> What's the functional difference between putting it as a type list and
>>> putting it as code?
> Not sure that I understood your question correctly, but if you meant
> differences between dependencies in the static_dependencies list and
> dependencies
> caused by calling initialization from main, then the difference is clear:
> if we put dependencies B and C to static_dependencies list when declaring
> class A, then only class A declaration becomes dependent on B and C. But if
> we require a call to A::initialize, B::initialize and C::initialize from
> main, then main becomes dependent on A, B and C. This way your startup code
> will be dependent on everything like that, that's not right.
Not from main, but from A's constructor or initialize function.
>
>>> If you want it to be initialized before main, then use global variables or
> static members.
> Ok, try to answer your question yourself:
>
> struct A { static A& instance() { ... } }
> struct B { static A& instance() { ... } }
> struct C { static A& instance() { ... } }
>
> ...
> static A& forceAConstr = A::instance();
> static B& forceBConstr = A::instance();
> static C& forceCConstr = A::instance();
> ...
>
> int main() {
> ...
> }
>
> Can you guarantee the order of construction for A, B and C in this case
> when standard explicitly states that the order of initialization is not
> determined?
Not with the code as written, but you can rewrite it to enforce it.
> In my code you can find a declaration and initialization of bool
> static_constructible<T, Dependencies>::constructed. So, if this variable is
> not referenced in the code it never get initialized.
Of course.
But if this is never referenced, then you cannot observe whether it has
been initialized or not.
>>> A given order can still be forced.
> Unless it's through compile configuration, provide an example please.
> Otherwise, having long lists of initialization orders for all supported
> compilers is error prone. The code itself has to describe itself as much as
> possible.
Simple: make the dependency explicit.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk