Operator Presedence and Associativity

← Back to the blog

What is an operator

An operator is a mathematical operation on one or many operands (targets to be used in the operation), which then produce a result. For now, let us assume that this is a singular value.

Operator presedence

In an expression where we have multiple cascading operations, using mulitiple operators, which consume the output from the other opartions. For example if we have

2 + 2 * 3

In a mathematical sense, this is the operation

2 + (2 * 3)

Meaning that first we do the multiplication, then the result from the multiplication is taken into the addition operation. Giving the correct result of 8.

So the Presedence is the priority of a given operator. Meaning that in the given expression, the multiplication operator has higher presedence than the addition operator.

Operator Associativity

Another place, where the order can get messed up is if we have an expression with multiple operators of the same priority. For instance, how is this expression supposed to be parsed and evaluated?

7 -2 -3

Should this be

(7 - 2) - 3 = 2

or

7 - (2 - 3) = 8

The order makes a difference here. Which takes us to the concept of associativity. Associativity means that, faced with multiple operators of the same precedence level next to eachother. In which order should the operators be evaluated. This is what is referred as left-to-right, or right-to-left associativity.

In this instance, the associativity of the subtraction operator is left-to-right. Meaning that the correct grouping of the operations is

(7 - 2) - 3 = (5 - 3) = 2

Now, why can we resolve operations precedence with parenthesis `()`. Well it is because the parenthesis has a higher presedence than all the operators of course!

Of note

The presedence of function argument evaluation order is undefined, meaning the compiler is free to do what it wants. This is what can lead to races, with for example, unique_ptr(new T())

Operator Classes

Unary operators

+

+x = x;

-

-x = -x; // The negation of x

Binary Operators

a + b; // Takes two operands

Modifying and non-modifying operators

An operator can be either modifying, or non-modifying, meaning that =, which changes the value of it's left-side operand, is modifying, while + is not.

Increment decrement operators and side effects

--x; // vs x-- // is different

In that the postfix will make a copy first, then increment. So it is less effective.

Thus, the =, and the ++ operators, are operators with side-effects, as they modify the state of the program, not only doing a calculation.

The Comma (,) operator

The comma operator allows you to evaluate multiple expressions wherever a single expression is allowed.

The ternary operator

<bool> ? <true-expr> : <false-expr> // The infamous

So C++ has an operator for ternary expressions. That is an operator taking three operands

Note that the type of the expression must match, or be convertible to eachother by the compiler. In general, here be dragons, so try and keep things explicit.