bool operator ++ and --

后端 未结 4 705
遥遥无期
遥遥无期 2020-11-29 01:03

Today while writing some Visual C++ code I have come across something which has surprised me. It seems C++ supports ++ (increment) for bool, but not -- (decrement). It this

4条回答
  •  温柔的废话
    2020-11-29 01:25

    ANSI ISO IEC 14882 2003 (c++03):

    5.2.6-2

    The operand of postfix -- is decremented analogously to the postfix ++ operator, except that the operand shall not be of type bool. [Note: For prefix increment and decrement, see 5.3.2. ]

    And unsurprisingly...

    5.3.2-2

    The operand of prefix -- is modified by subtracting 1. The operand shall not be of type bool. The requirements on the operand of prefix -- and the properties of its result are otherwise the same as those of prefix ++. [Note: For postfix increment and decrement, see 5.2.6. ]

    Also the 5.6.2-1 and 5.3.2-1 mention that ++ for bools shall be true and Annex D-1 says that ++ on bools in deprecated.

提交回复
热议问题