https://gcc.gnu.org/bugzilla/show_bug.cgi?id=4131

--- Comment #29 from Kenneth Almquist <kalmquist1 at hotmail dot com> ---
(In reply to Jorg Brown from comment #20)
> Now, the order of construction is well-defined, and that is why the program
> produces:
> 
> podStr = '*'
> nonpodStr = ''
> 
> That is, the nonPOD is still zero-filled when the constructor for nonpodStr
> runs, so nonpodStr ends up empty.

Actually, looking at paragraph 2 of section 3.6.2 of the 1997 draft standard,
it appears that the order of construction is not entirely well defined. 
Consider:

     extern int x, y, z;
     int z = y + 1;
     int y = x + 1;
     int x = 10;

As I understand it, x must use static initialization for x, but has the option
of using either static or dynamic initialization for y and z.  Normally,
variables are initialized in program order, so that z would be initialized
before y.  However, if the compiler choses dynamic initialization for z and
static initialization for y, then z will be initialized after y.  Similarly, in
your example, nonpodStr could be either '' or '*'.

There is one special rule in evaluating the initial value of variables
when static initialization is used:

  If the expression contains a variable
  and the compiler is allowed to use dynamic initialiation for that variable
  and that variable is initialized later in the program
  then zero is used as the value of the variable.

In the above example, if static initialization is used for z, the initial
value will be 1, because zero will be used as the value of y when computing
y + 1.  On the other hand, if static initialization is used for y, the
initial value of y will be 11, because the compiler is require to use
static initialization for x and the rule given above doesn't apply.

In short, computing the values of initializers at compile time for C++ involves
a special rule which, as far as I know, doesn't apply to any other language. 
For that reason, it might make sense to do this calculation in the C++ front
end.  I think that the front end already does a bit of this, but only for
expressions that are formally constant expressions.

Reply via email to