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?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
  • 2
    @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. – Kerrek SB Jun 20 '11 at 16:01
  • 2
    Since this universally considered bad practice why worry about it. Just put each declaration in a separate statement. – Martin York Jun 20 '11 at 16:18
  • 2
    @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::type x, * px = &x;`... – Kerrek SB Jun 20 '11 at 16:36
  • 2
    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. – Johannes Schaub - litb Jun 20 '11 at 16:38
  • @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). – Martin York Jun 20 '11 at 17:33

2 Answers2

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;
Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • 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;` – Armen Tsirunyan Jun 20 '11 at 16:17
  • +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. – Johannes Schaub - litb Jun 20 '11 at 16:19
9

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

Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
  • 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. – Kerrek SB Jun 20 '11 at 16:12