Pretty clear in the title, I think. I'm not entirely sure on this, and I can't find a good answer via the Googles (alas, I haven't committed to the fine art of standards-fu), so I ask:
int i = x++, j = x++;
Is this defined? I am quite sure that i = x++, j = x++;
as a normal statement would be undefined behavior is the comma operator, which is a sequence point and would be legal, but no source is quite clear on whether an initializer ends at the semicolon or once the next variable starts being declared, and since that's not the comma operator in use I can't find a clear answer. So either a) the comma ends the initializer, is a sequence point, and that works, or b) it doesn't. Which is it?
And to preclude, I know I should simplify the headache and just write it as:
int i = x++;
int j = x++;
And guarantee that it's defined. I'm asking more out of curiosity.
The end of an initializer is a sequence point so the example in the title is legal.
The comma operator is also a sequence point so that your "normal statement" is also legal and well-defined.
The wikipedia article has a list of the C and C++ sequence points.
To follow up on a comment below, here's a demonstration of the fearsome power of the comma operator, as preserved in FreeBSD's stdio.h (under ifndef __GNUC__
):
/*
* This has been tuned to generate reasonable code on the vax using pcc.
*/
#define __sputc(c, p) \
(--(p)->_w < 0 ? \
(p)->_w >= (p)->_lbfsize ? \
(*(p)->_p = (c)), *(p)->_p != '\n' ? \
(int)*(p)->_p++ : \
__swbuf('\n', p) : \
__swbuf((int)(c), p) : \
(*(p)->_p = (c), (int)*(p)->_p++))
#endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With