Like everyone else, I assume that the line you are thinking of is this:
int array1 = {1,2,3,4,5};
In this case, there is no difference between C and C ++; the line may be legal in both languages, but that doesnβt mean what you think. In C and C ++, there is a statement that if a type is a scalar type ( int is), then the content {...} should be a single expression. And 1,2,3,4,5 can be interpreted as a single expression (with a comma operator); something like:
int array1 = 1, 2, 3, 4, 5;
clearly legal.
This is somewhat ambiguous, however, since in both languages ββthe grammar for this type of initialization does punctuation rather than an operator. So this is a matter of interpretation; this statement that the contents should be the only expression is a restriction on the grammar (which will lead to the comma becoming an operator) or a restriction on the results of the evaluation of the specified grammar. My intuitive feeling is that the second is intention, and this statement should lead to error. But the difference is not between C and C ++, but between the way compiler authors interpret the standard.
EDIT:
Rereading a little closer: in the C ++ standard, he clearly says that
If T is a scalar type, then the declaration of the form
T x = {a};
equivalently
T x = a;
Which does not leave much room for disassembly: in C ++, the expression seems clearly legal; it is only in C where there is some ambiguity.
James Kanze Oct 29 '14 at 11:54 on 2014-10-29 11:54
source share