How is the expression x---y parsed? Is it a legal expression?

后端 未结 4 1245
失恋的感觉
失恋的感觉 2020-12-20 23:25

How is the expression x---y parsed? Is it a legal expression?

相关标签:
4条回答
  • 2020-12-21 00:04

    This is related to operator precedence. Have a look at this table.

    The decrement/increment operator has precedence over the arithmetic operations. It will be parsed as x-- - y.

    To correct my answer: The parser matches the longest token first, so -- is chosen over the arithmetic -. Resulting in the expression being parsed as x-- - y

    0 讨论(0)
  • 2020-12-21 00:07

    It's legal and parsed as x-- - y.

    I believe the first two minus signs are interpreted as a post-decrement operator because it's the longest token following x that is legal to appear. This leaves the third minus to play the role of subtraction. This is in accordance with the Maximal Much Rule[1]

    [1]. http://en.wikipedia.org/wiki/Maximal_munch

    0 讨论(0)
  • 2020-12-21 00:19

    (x--)-y; Which compiler do you use? What are the types of x and y?

    0 讨论(0)
  • 2020-12-21 00:24

    For all data types, it's parsed as x-- - y. If it's some class object then you have to define post decrement operator and minus operator, it will give compiler error if you just define pre decrement operator. That means, x-- - y is forced in any case.

    0 讨论(0)
提交回复
热议问题