Is the comma in a variable list a sequence point?
Asked Answered
F

3

23

In the following type of code is there a sequence point between each variable construction, or is the result undefined?

int a = 0;
int b = a++, c = a++;

I wasn't able to find in the standard a specific reference to a sequence point here. Does that mean it is undefined, or just that I failed in my search? The completion of an expression is a sequence point, but does the above initialization also count?

Fadden answered 20/6, 2011 at 15:55 Comment(5)
@Mark: Correct, the comma is a separator, not an operator in that situation. Update: Where did Mark go? :-S I'd hazard a guess that since it's a separator it is definitely a sequence point, but I'm eagerly waiting for an authoritative answer.Fireball
Since this universally considered bad practice why worry about it. Just put each declaration in a separate statement.Swedenborgianism
@Martin: I sometimes find it useful for making a variable and a pointer to it in one go, especially if the typename is really long: MyVeryLongType::subclass::foo<Bar>::type x, * px = &x;...Fireball
I always do stuff like for(iterator b = begin(), e = end(); ..) and I think it's fine. Just don't do side effects in the initializers.Rubricate
@Kerrek SB: Use two lines (don't be lazy). You will find almost universally that any company with coding guidelines will force you to re-write it anyway (get used to it). As litb mentions about the only place it is tolerated is in for(;;) where it is universally accepted as ok (bu never for doing what you do).Swedenborgianism
C
27

I believe behavior is well-defined because of 8[dcl.decl]/3

Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself.

Which is even additionally explained in a footnote as

A declaration with several declarators is usually equivalent to the corresponding sequence of declarations each with a single declarator. That is

T D1, D2, ... Dn;

is usually equvalent to

T D1; T D2; ... T Dn;
Cilla answered 20/6, 2011 at 16:11 Comment(2)
+1: The word usually means that they may not be equivalent, for example, in this case: struct S{}; S S, A; (not equivalent to the ill-formed S S; S A;Reexamine
+1, but take note that this is only half of the cake. Only 8p3 together with 1.9px seems to completely answer that (one could still argue that "int a = i++; int b = i++;" is UB because of a missing sequence point, if it weren't 1.9px to forbid this argument). Therefor I +1 both @Alan and your answer.Rubricate
A
10

As you suspect there is a sequence point after each initializer expression, because they're full expressions (1.9/16, 1.9/12).

Astyanax answered 20/6, 2011 at 16:7 Comment(1)
Could you add whether the OP's example is equivalent to int b = a++; int c = a++;? Update: Never mind, Cubbi's answer nails it.Fireball
C
0

The answers above seem to pertain to C++. Just to confirm, for C, ISO/IEC 9899:TC3 committee draft (September 7, 2007) Annex C, says:

The following are the sequence points described in 5.1.2.3: [...]
— The end of a full declarator: declarators (6.7.5);
— The end of a full expression: an initializer (6.7.8); [...]

So the answer is the same:

there is a sequence point after each initializer expression, because they're full expressions

Ceratodus answered 3/11, 2023 at 0:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.