2

Exercise 4.20 of C++ Primer, 5e asks whether the expression iter++->empty(); is legal. Assume that iter is a vector<string>::iterator.

This expression is legal. I compiled it with gcc, and the answers to another question on Stack Overflow have addressed this much. However, I'm confused as to why it is legal.

This answer to a similar question gives the following as an equivalent pair of expressions:

iter->empty();
iter++;

The operator precedence table in my book lists -> as having higher precedence than the postfix ++ operator. This matches the explicit order of operations in the equivalent code above. However, I am used to seeing operators apply to whatever is right next to them. In the case of ->, I expected the compiler would to apply it to ++ (by itself, without iter) and throw an error. In other words, I tried to parenthesize the original expression as iter(++->empty());, which is obviously illegal.

So, it seems like c++ requires compilers to parse expressions in a more complex way than just parenthesizing based on precedence and associativity. Is that right? If there is an easy way to explain how this actually happens, I would like to know about it.

10
  • 1
    Why do you think it shouldn't be? Which container type is iter from? Dec 29, 2020 at 18:55
  • 1
    My table says they got equal precedence. Dec 29, 2020 at 18:57
  • 2
    Do be careful to avoid confusing "legal" as in "compiles because it is syntactically valid" with "legal" as in "has defined behaviour".
    – tadman
    Dec 29, 2020 at 18:57
  • I would think the "obviously illegal" approach you think is logical would be a strong argument for why it doesn't do that; that attempt at parsing doesn't make sense, since you can't attach an operator to an operator, it has to attach to an expression, and so iter++ has to bind first. Also, your book is wrong, or you're misreading it; postfix ++ has the same precedence as -> (prefix ++ has lower precedence than either, but it's not involved here). Dec 29, 2020 at 18:57
  • 1
    I've looked at the precedence table in the C++ Primer and it is definitely wrong and even meaningless on several counts. Postfix ++ has right associativity? Really? I'd like to try what they were smoking. How do you ever interpret this statement?
    – n. m.
    Dec 29, 2020 at 19:28

1 Answer 1

5

Per cppreference, ++ and -> have the same precedence and have left to aright associativity. That means that iter++ is executed first, and then ->empty() is applied to the result of iter++, which is just iter (from before the increment) since it is postfix increment.

12
  • Interesting, thanks for the link. This contradicts the table page 166 of C++ Primer, 5th edition. Any chance this was a recent change to the standard? Dec 29, 2020 at 19:01
  • @KevinBradner AFAIK, it's always been like this. Dec 29, 2020 at 19:02
  • @KevinBradner Having different precedence for different postfix unary operators wouldn't make a lot of sense (same for prefix ones). Dec 29, 2020 at 19:04
  • I was assuming it had to do with associativity. According to my reference, (->) is left associative, and postfix (++) is right associative. I'm not sure how these resolve if the operators are at the same precedence level. Dec 29, 2020 at 19:06
  • 2
    @KevinBradner Thanks. Looks like a bad table to me. Dec 29, 2020 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.