Search code examples
c++operator-precedence

How Precedence of Relational Operator handled in C++


According to precedence rules <, >, <=, >= has precedence over !=, == I am so confused that how the following statement will be executed

int a=3, b=3;
cout<< (a != b || a <= b);

I know short circuit evaluation and according to precedence rules I guess that compiler will execute a <= b first as it has precedence over != but it is not doing so.

I did little experiment with above statement by changing a <= b-- and changing order of above conditions and it seems that <= and != have same precedence as compiler execute whichever occurred first. Or I am missing something?


Solution

  • Precedence of operators is only relevant to how expressions are bound, not to how they're executed. Execution order is dependent on the "happens-before" relationship and otherwise subject to arbitrary reordering by the compiler.

    Relative precedence of two operators also only matters if they are directly adjacent. In a == b <= c, you get a == (b <= c), not (a == b) <= c. In a == b || c <= d, the adjacent pairs are == and || and || and <=. In both cases the comparison operators bind more strongly, so you get (a == b) || (c <= d), making the relative precedence of == and <= irrelevant. If you have a == b + c <= d instead, you first get a == (b + c) <= d, and now you need to compare == and <= again, getting you a == ((b + c) <= d).

    As for order of evaluation, || has a rule that its left side is sequenced before its right side (assuming it's not overloaded), because the right side might not get evaluated at all. So the == is executed first. But precedence plays no part at all in this. If you instead had written the non-short-circuiting a != b | a <= b, both sides would eventually get executed (with caveats, see below), but there are no guarantees which side gets evaluated first; precedence does not play a part here.

    Caveat: the compiler can still just optimize your code and realize that a != b || a <= b is tautological, and simply replace the entire thing with true.