c++arraysinitializationlanguage-lawyerdefault-initialization

Is the Order of Array Element Default-Initialization Defined?


When an array is default initialized, is the order in which it's elements are default initialized defined by the C++ standard?

To give an example, is the following C++ program guaranteed do print strictly ascending memory addresses?

#include <iostream>
struct Foo{
    Foo() {
        std::cout << this << std::endl;
    }
};
int main() {
    delete[] new Foo[10];
    return 0;
}

I found the following in the Standard under 9.4.1 Initializers, General:

7 To default-initialize an object of type T means: (...)

(7.2)— If T is an array type, each element is default-initialized.

Am I correct in assuming that this means that it's unspecified, or is this clarified somewhere else in the document?

This answer claims that It's defined (under (a)), but (afaict) does not provide any evidence for that.

Note that I'm not trying to refer to the array itself before it is initialized (Can a (C/C++) array initialization reference itself?), but simply care about observable side effects of the initialization order.


Solution

  • The latest draft (N4917) has this to say under 9.4.2 Aggregates, item 7 ([dcl.init.aggr]):

    The initializations of the elements of the aggregate are evaluated in the element order. That is, all value computations and side effects associated with a given element are sequenced before those of any element that follows it in order.

    So you can depend on the order.