Is this list-initialization of an array of unknown size valid in C++0x?
int main() { int x[]{0, 1,2,3,4}; return x[0]; }
I believe it is valid, but would appreciate some confirmation.
If anyone could quote from the C++0x-FCD to support their case, it would be greatly appreciated.
Thanks!
This goes from
8.5/16first bullet to8.5.4list-initialization and from8.5.4/3third bullet to8.5.1aggregate initialization and then8.5.1/4saysThe only difference if the object is an array between
= { ... }and{ ... }is that the first is called copy-list-initialization and the second is called direct-list-initialization, so both are kinds of list-initialization. The elements of the array are copy-initialized from the elements of the initializer list in both cases.Notice that there is a subtle difference between those forms if the array has a size and the list is empty, in which case8.5.4second bullet applies:This difference does not apply to lists that have content in which case third bullet applies again, though
The FCD changed this compared to the previous draft, and initialization with an empty initializer list now always works even with explicit default constructors. This is because the FCD states that the elements are value-initialized, and value initialization doesn’t care about explicitness since it doesn’t do overload resolution on default constructors (it couldn’t figure out better or worse matches anyway). The previous draft used normal overload resolution on the constructors and thus rejected explicit default constructors during copy initialization. This defect report did that change.