views:

210

answers:

2

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!

A: 

Yes, it is valid, and has been for decades, even in C. The size is simply set to the number of elements supplied. I don't know the reference, unfortunately.

(Added bonus...) If you need the number of elements use sizeof(x)/sizeof(*x). It's safer than hard-coding a constant that may become invalid if you add or remove entries.

EDIT: As pointed out in the comments, the code in question is missing an = (a fact that I missed), without which it isn't valid in any current standard of C or C++.

Marcelo Cantos
Wrong. Without the = sign, it is only valid in C++0x (not in C++03).
SoapBox
+3  A: 
Johannes Schaub - litb
Thanks :) Interesting (but very subtle) point about difference btw initializing with an empty list vs a non-empty list. I wonder if symmetry there would be a better thing?
Faisal Vali
Also do you know if:int (
Faisal Vali
@Faisal, notice that also `explicit A(int = 0);` is an explicit default constructor. So this change could affect more programs than one might think first. But i think that the behavior of the FCD is more beneficial, since "explicit" doesn't really say something about default construction, but more about the argument conversions :)
Johannes Schaub - litb
@Faisal, the situation with array references currently in the Standard is a bit weird. See for instance http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1058 . For your example, we hit "Otherwise the program is ill-formed.". There is some other case for `int(` which hits "... or if T is any reference type and the initializer list has no elements", but it's illformed because it tries to construct a zero-element array.
Johannes Schaub - litb
@Faisal, and for `int(" which however sounds weird because it seems to imply the case can only occur for objects, but in fact in this case it occurs for references. In any case if we replace "object" by "object or reference", then it tries to do `int(` which is ill-formed likewise. So in any case, you cannot do that, but it seems the Standard isn't well thought out about array references.
Johannes Schaub - litb
Faisal Vali
I think i'm going to try and post a comment [here](http://www.justsoftwaresolutions.co.uk/cplusplus/c++0x-now-at-fcd.html#makecomment) so that the committee might try and clean this up. Although you might be more qualified to make the comment.
Faisal Vali
Johannes Schaub - litb
@Johannes - Thanks again for your rigorous insight. To all others who read this comment, please up vote the answer - the issues addressed by the answer and the author's ensuing comments show deep insight of a difficult topic. I wish i could upvote it a few more times ;)
Faisal Vali
@Faisal, thanks for the praise :) I think i'm not a good commenter on this array-reference issue because i have doubts the committee would be willing to support it (they commented on my issue report saying that they generally want to avoid array temporaries).
Johannes Schaub - litb