为什么定义 bool 上的“++”运算符?但是“--”不在 C++ 中

Why "++" operator over bool is defined?? however "--" is not in C++

我想知道为什么 ++ 运算符是在 bool 上定义的...但是当我尝试 -- 运算符时,它没有为 bool 定义。 .

谁能解释一下这背后的原因吗?

参考文档: https://msdn.microsoft.com/en-us/library/tf4dy80a.aspx

When a postfix or prefix ++ operator is applied to a variable of type bool, the variable is set to true. The postfix or prefix -- operator cannot be applied to a variable of this type.

来自标准 5.2.6.1

The value of the operand object is modified by adding 1 to it, unless the object is of type bool, in which case it is set to true. [ Note: this use is deprecated, see Annex D. —end note ]

和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. —end note ]

和附件D

D.1 Increment operator with bool operand [depr.incr.bool] 1 The use of an operand of type bool with the ++ operator is deprecated (see 5.3.2 and 5.2.6).

所以反过来你不能的原因是因为它违反了标准。