Topic: Static object initialization
Author: kanze@us-es.sel.de (James Kanze)
Date: 01 Feb 1994 18:21:19 GMT Raw View
In article <CK9Gnv.3xys@hawnews.watson.ibm.com> tedlaw@vnet.ibm.com
writes:
[Comments concerning the order of initialization deleted...]
|> At this point, my conclusion is that the ARM is not saying exactly
|> what it wants to say. I don't know even know what it intends to say.
|> I am not a language lawyer. Am I misinterpreting anything?
No. The order of initialization is a real problem, and it is
generally recognized that the statement in the ARM is not
implementable. The standards committee is trying to find a solution,
but this isn't an easy problem.
For the moment, your best bet is to make no assumtions concerning
order of initialization.
--
James Kanze email: kanze@us-es.sel.de
GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
Author: tedlaw@vnet.ibm.com
Date: Wed, 26 Jan 1994 23:45:31 GMT Raw View
Section 3.4 of the ARM says:
> The initialization of nonlocal static objects in a translation unit is
> done before the first use of any function or object defined in that
> translation unit. Such initializations may be done before the first
> statement of main() or deferred to any point in time before the first
> use of a function or object defined in that translation unit.
Consider the following translation unit:
void f() {}
class X {
public:
X() { f(); }
...
};
static X x;
`x' is a nonlocal static object defined in this translation unit.
According to the first sentence of the quoted paragraph, the
initialization is done before the first use of any function or object
defined in that translation unit. `f()' is such a function. (We may
even say X::X() is also such a function, if we agree that a constructor
is a function.) But since the constructor calls `f()', this results in
a contradition (if `f()' is also called from outside the translation
unit): The initialization of `x' must be done before any use (call) of
`f()', and the initialization of `x' cannot be done (complete) before
`f()' is called (from within X::X()). It seems unreasonable to deem
this program illegal just because of the contradiction in the ARM.
The second sentence of the quoted paragraph seems to escape such
contradition: Since there is no way to complete the initialization of
`x' before the first use of calling `f()', we can just initialize it
before the first statement of main(). However, I doubt that this is
the original intention of the ARM.