Random Number Order in C++ using

x << y is syntactic sugar for a function call to operator<<(x, y).

You will remember that the c++ standard places no restriction on the order of evaluation of the arguments of a function call.

So the compiler is free to emit code that computes x first or y first.

From the standard: ยง5 note 2:

Operators can be overloaded, that is, given meaning when applied to expressions of class type (Clause
9) or enumeration type (7.2). Uses of overloaded operators are transformed into function calls as described
in 13.5
. Overloaded operators obey the rules for syntax specified in Clause 5, but the requirements of
operand type, value category, and evaluation order are replaced by the rules for function call.

Leave a Comment