Home > Enterprise >  Operator precedence - increment versus member access
Operator precedence - increment versus member access

Time:04-13

Consider this:

  iterator->some_value

Will the iterator be forwarded before some_value is accessed?

According to cppreference increment and member access both have the same precedence of 2. Does the order in which they are listed matter? Or is this undefined - compiler specific?

CodePudding user response:

The member access operator -> has higher precedence over the prefix increment operator. Thus the expression iterator->some_value is grouped as if you had written

  (iterator->some_value)

On the other hand, if your were to write the expression iterator->some_value then since both the member access operator -> and the postfix increment operator have the same precedence, so left-to-right associativity will be used, which will make this expression equivalent to writing :

(iterator->some_value)  

CodePudding user response:

Note that preincrement and postincrement have different precedences, so the code snippet you've posted doesn't quite match the explanatory text you've linked.

The preincrement operator has a lower precedence than the member access operator, so iterator->some_value is the equivalent of (iterator->some_value).

If instead you had iterator->some_value where they both had the same precedence, then the left-to-right associativity comes into effect, and it would be processed as (iterator->some_value) .

  • Related