On 04/02/2021 21:08, AJ D via Gcc wrote:
> Isn't comma operator suppose to honor left-to-right associativity?
> 
> When I try it on this test case, it exhibits right-to-left associativity.

You are not talking about associativity - you are talking about
evaluation order.  (The two things are often mixed up.)

For the built-in comma operator, you get guaranteed order of evaluation
(or more precisely, guaranteed order of visible side-effects).  But for
a user-defined comma operator, you do not - until C++17, which has
guaranteed evaluation ordering in some circumstances.

See <https://en.cppreference.com/w/cpp/language/operator_other> (it's
easier to read than the C++ standards).

Try your test again with "-std=c++17" or "-std=g++17" - if the order is
still reversed, it's a gcc bug (AFAICS).  But for standards prior to
C++17, the ordering is unspecified for user-defined comma operators.

This is not unlike the difference between the built-in logic operators
&& and ||, which are guaranteed short-circuiting, while user-defined
overloads are not.  And for arithmetic operators, you don't get integer
promotion, automatic conversion to a common type, etc.

Basically, the user-defined operators are just syntactic sugar for a
function call - they don't have the "magic" features of the real operators.

David

Reply via email to