Re: static data member
On Nov 20, 12:46 am, "Alf P. Steinbach" <al...@start.no> wrote:
* Craig Scott:
[...]
I'm wondering then what 6.7/4 means:
"... An implementation is permitted to perform early initialization of
other local objects with static storage duration under the same
conditions that an implementation is permitted to statically
initialize an object with static storage duration in namespace
scope... "
"under the same conditions", i think a check of the standard
will show that that means no side effects etc., that the
object can be initialized completely by a simple raw memory
copy of data computed at compile time.
in effect, i think a check of the standard will show that the
only such early initializations are those that you cannot
detect except by measuring execution time or tracking the
machine code execution.
Yes. I'm not sure of the exact words (and I'm too lazy to look
them up now), but I'm pretty sure that the intent here is to
allow the compiler to use static initialization for things like
complex<double>; a fairly simple analysis of the constructor
allows it to determine the actual bit pattern, and determine
that there are no side effects.
Note that this optimization requires special authorization in
general case, since a conforming program can tell:
#include <complex>
extern int f() ;
int i = f() ;
std::complex<double> z( 1.2, 3.4 ) ;
int
f()
{
return static_cast< int >( z.real() ) ;
}
(Here, i will be initialized with 0 if z is dynamically
initialized, but with 1 if it is statically initialized.)
Off hand, I can't come up with a similar example involving local
statics, but I wouldn't swear that one doesn't exist, either.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34