Operator hierarchy in ++structure.field

Viewed 64

The example:

++structure.field;

increments field instead of giving "wrong type argument to increment" compiler error, although ++ and . operators are equaly hierarchized ergo: should've been executed from left to right.

Am I missing something here?

1 Answers

Prefix and postfix ++ have different precedence. . has the higher precedence than the prefix increment operator, as seen on cppreference.com.

Excerpt of the operator precedence and associativity table from cppreference.com

. and postfix increment have the same precedence. If you wrote structure.field++ then they'd have the same precedence and associativity would kick in to resolve the ambiguity as (structure.field)++ rather than structure.(field++).

Related