Is "new int[8]()" equivalent to "new int[8]{}" in C++11?
Asked Answered
E

1

13

Is new int[8]() equivalent to new int[8]{} in C++11?

In other words:

Does the C++11 standard guarantee each of new int[8]() and new int[8]{} returns a zero-initialized array?

Essence answered 26/12, 2016 at 6:34 Comment(3)
The more of these questions I read, the more baffled I become at why the committee ever thought the brace syntax was a good idea...Strappado
@Mehrdad: So... what, you'd rather not be able to initialize an array you heap-allocate with values?Rauwolfia
@NicolBolas: When was the last time you needed to do that? What did you use to do before C++11?Strappado
R
16

new int[8]() will, by [dcl.init]/17.4, be value-initialized. Since it is an array, [dcl.init]/8.3 tells us that value initializing an array means to value-initialize each element.

new int[8]{} will, by [dcl.init.list]/3.2, invoke aggregate initialization on the array. Since there are no elements in the braced-init-list, each of the remaining elements in the array (ie: all 8) will be initialized "from an empty initializer list" ([dcl.init.aggr]/8). Which, after dancing through [dcl.init.list] again, leads you to 3.4, which tells you that "from an empty initializer list" for non-aggregate types means value-initializiation.

So yes, they both evaluate to the same thing.

Rauwolfia answered 26/12, 2016 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.