Does the comma operator have to be left-associative?
Asked Answered
B

2

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?

Broadway answered 23/12, 2012 at 11:29 Comment(16)
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).Exciseman
How is your comment different from @Pubby's answer?Broadway
Why it has to be different? 2+3 has same answer for all.Exciseman
Because when you come later to the table, you have to add something new ;)Broadway
Not necessarily. Also, I didn't come later. Just didn't refresh the page.Exciseman
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.Breazeale
@AlexisWilke: You're wrong. In C++, the order of function arguments evaluation is not specified. So they can be evaluated in any order.Exciseman
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 they aren't even supposed to).Perdu
(To late to edit... I meant (a * b) * c and a * (b * c) for octonions, of course)Perdu
@leftaroundabout: No worry. Your comment makes sense for + as well.Exciseman
@Perdu 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?Broadway
@Perdu 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.Broadway
Also, I found a somewhat related question that people may find interesting :)Broadway
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.Khalsa
@MathieuBorderé No, (printf("a"), printf("b")), printf("c"); and printf("a"), (printf("b"), printf("c")); both print "abc".Broadway
@Broadway thanks, associativity != order of evaluation, lesson learned.Khalsa
U
13

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

Ultramicrochemistry answered 23/12, 2012 at 11:32 Comment(0)
D
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

Downrange answered 6/8, 2013 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.