Topic: Question on example to C++03 3.6.2/2
Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Sun, 18 Jul 2010 15:28:39 CST Raw View
I'm not fully understanding the implications of this example:
inline double fd() { return 1.0; }
extern double d1;
double d2 = d1; // unspecified:
// may be statically initialized to 0.0 or
// dynamically initialized to 1.0
double d1 = fd(); // may be initialized statically to 1.0
I think this is what goes on:
---------------
Analysis of d2
* "= d1" does not change any other static storage variable
* When both d2 and d1 are initialized dynamically, then d2 would be
initialized to 0.0, because d2 is defined before d1, and dynamic
initialization of d2 would grab the value of d1 as of the state just after
static initialization (where only zero initialization of d1 took place).
Analysis of d1
* "= fd()" does not change any other static storage variable
* When both d2 and d1 are initialized dynamically, then = fd() will
initialize d1 to 1.0.
---
So, the compiler may initialize d1 statically to 1.0, because both
conditions for optional-static-initialization are met.
* If the compiler decides to initialize d1 and d2 dynamically, then d2 will
be initialized to 0.0, since it will grab the value of d1 as it was just
after zero initialization.
* However, if the compiler decides to initialize d1 statically and d2
dynamically, then d2 will be initialized to 1.0, since the dynamic
initialization of d2 will grab the fully initialized value of d1 as it was
just after static initialization.
---------------
But i don't understand what happens if the compiler decides to initialize
*both* d1 and d2 statically. Since static initialization has no order, what
value of d1 is grabbed by d2? The 1.0 of copy initialization or the 0.0 of
zero initialization?
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Mon, 19 Jul 2010 12:24:11 CST Raw View
On 18 Jul., 23:28, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
> I'm not fully understanding the implications of this example:
>
> inline double fd() { return 1.0; }
> extern double d1;
> double d2 = d1; // unspecified:
> // may be statically initialized to 0.0 or
> // dynamically initialized to 1.0
> double d1 = fd(); // may be initialized statically to 1.0
>
> I think this is what goes on:
>
> ---------------
> Analysis of d2
>
> * "= d1" does not change any other static storage variable
> * When both d2 and d1 are initialized dynamically, then d2 would be
> initialized to 0.0, because d2 is defined before d1, and dynamic
> initialization of d2 would grab the value of d1 as of the state just after
> static initialization (where only zero initialization of d1 took place).
<nod>
> Analysis of d1
>
> * "= fd()" does not change any other static storage variable
> * When both d2 and d1 are initialized dynamically, then = fd() will
> initialize d1 to 1.0.
>
> ---
>
> So, the compiler may initialize d1 statically to 1.0, because both
> conditions for optional-static-initialization are met.
I agree so far.
> * If the compiler decides to initialize d1 and d2 dynamically, then d2 will
> be initialized to 0.0, since it will grab the value of d1 as it was just
> after zero initialization.
Yes.
> * However, if the compiler decides to initialize d1 statically and d2
> dynamically, then d2 will be initialized to 1.0, since the dynamic
> initialization of d2 will grab the fully initialized value of d1 as it was
> just after static initialization.
Yes.
> But i don't understand what happens if the compiler decides to initialize
> *both* d1 and d2 statically. Since static initialization has no order, what
> value of d1 is grabbed by d2? The 1.0 of copy initialization or the 0.0 of
> zero initialization?
I read bullet 2 of 3.6.2/3 that it requires that d1 would be zero-
initialized
in this case.
I don't understand what you mean with "Since static initialization has
no
order" in this context. Surely static initialization introduces
dependencies
among values and this induces an order (even though this is no run-
time
ordering), e.g. in the example:
constexpr int i1 = 42;
constexpr int i2 = i1;
Of-course i2 is ordered with the value produced for i1 and thus will
require
that i2 is initialized with 42 (and not with 0, for example). I agree
that zero-
initialization can be considered to be free of order, because it
essentially
decouples all variables from each other.
So, if aforementioned bullet 2 requires
"the static version of the initialization produces the same value in
the
initialized variable as would be produced by the dynamic
initialization
if all variables not required to be initialized statically were
initialized
dynamically."
this must result in a value of 0 for d2, because this would be it's
value, if d1 were initialized dynamically.
HTH & Greetings from Bremen,
Daniel Kr gler
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]