I was wondering why the ++
operator is defined over bool
... however when I tried the --
operator, it was not defined for bool
..
Can someone please explain me the reason behind that?
I was wondering why the ++
operator is defined over bool
... however when I tried the --
operator, it was not defined for bool
..
Can someone please explain me the reason behind that?
Refer to the documentation: 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.
From the Standard 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 ]
And 5.2.6.2 emphasis mine
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 ]
And Annex 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).
So in turn the reason you can't is because it is against the standard.