11

According to this precedence table, the comma operator is left-associative. That is, a, b, c is parsed as (a, b), c. Is that a necessity? Wouldn't a, (b, c) have the exact same behavior?

Community
  • 1
  • 1
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • 2
    What if `a`, `b` and `c` are of different types, and each overloads `,` which returns a type which overloads `,` too? `(a,b),c` would not be same as `a,(b,c)`. – Nawaz Dec 23 '12 at 11:36
  • How is your comment different from @Pubby's answer? – fredoverflow Dec 23 '12 at 11:40
  • Why it has to be different? `2+3` has same answer for all. – Nawaz Dec 23 '12 at 11:42
  • Because when you come later to the table, you have to add something new ;) – fredoverflow Dec 23 '12 at 11:43
  • 1
    Not necessarily. Also, I didn't come later. Just didn't refresh the page. – Nawaz Dec 23 '12 at 11:44
  • As a side note, the parameters to a function being called are also following the same rule: left to right. However, Visual C++ (cl) compiles right to left in debug mode. Something to keep in mind. Although it is somewhat different from the command operator per se. – Alexis Wilke Dec 23 '12 at 11:45
  • 5
    @AlexisWilke: You're wrong. In C++, the order of function arguments evaluation is not specified. So they can be evaluated in any order. – Nawaz Dec 23 '12 at 11:47
  • 2
    Why would they be equivalent? Not even `(a + b) + c` and `a + (b + c)` are guaranteed to be equivalent. (They aren't in floating-point arithmetic for accuracy reasons, and in [some exotic number systems](http://en.wikipedia.org/wiki/Octonions#Properties) they aren't even _supposed to_). – leftaroundabout Dec 23 '12 at 12:08
  • (To late to edit... I meant `(a * b) * c` and `a * (b * c)` for octonions, of course) – leftaroundabout Dec 23 '12 at 12:29
  • @leftaroundabout: No worry. Your comment makes sense for `+` as well. – Nawaz Dec 23 '12 at 13:10
  • @leftaroundabout With the built-in comma operator (i.e. no user-defined overloads), both `(a, b), c` and `a, (b, c)` will evaluate `a`, `b` and `c` (in that order) and yield the result of `c`. How is that *not* equivalent? – fredoverflow Dec 23 '12 at 14:07
  • @leftaroundabout By the way, `(a + b) + c` and `a + (b + c)` are also not the same for signed integers in C++. For example `INT_MAX + (1 + -1)` is `INT_MAX`, but `(INT_MAX + 1) + -1` invokes undefined behavior due to signed integer overflow. – fredoverflow Dec 23 '12 at 14:20
  • Also, I found a [somewhat related question](http://stackoverflow.com/questions/7201522/) that people may find interesting :) – fredoverflow Dec 23 '12 at 14:21
  • right-associativity also feels unnatural, the comma operator will evaluate its left operand before the right, but suddenly when you involve multiple comma operators, you lose that behaviour and `(a, b, c);` would evaluate b then c then a. – Mathieu Borderé Aug 07 '17 at 09:52
  • @MathieuBorderé No, `(printf("a"), printf("b")), printf("c");` and `printf("a"), (printf("b"), printf("c"));` both print `"abc"`. – fredoverflow Aug 07 '17 at 19:38
  • @fredoverflow thanks, associativity != order of evaluation, lesson learned. – Mathieu Borderé Aug 08 '17 at 08:59

2 Answers2

13

Since overloadable operator, exists, no, it's not the same behavior. a, (b, c) could call different overloads than (a, b), c.

Pubby
  • 51,882
  • 13
  • 139
  • 180
1

The comma operator has left-to-right associativity. Two expressions separated by a comma are evaluated left to right. The left operand is always evaluated, and all side effects are completed before the right operand is evaluated.

Commas can be used as separators in some contexts, such as function argument lists. Do not confuse the use of the comma as a separator with its use as an operator; the two uses are completely different.

http://msdn.microsoft.com/en-us/library/zs06xbxh.aspx

Ryaan Dias
  • 31
  • 1
  • 6