Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Why does the operator -- not exist for bool whereas it does for operator ++?

I tried in C++, and I do not know if my question apply to another language. I will glad to know also.

I know, I can use the operator ++ with a bool. It makes any bool equal to true.

bool b = false;
b++;
// Now b == true.

Why can't we use the operator -- in an opposite way?

bool b = true;
b--;
// Now b == false;

It is not very useful, but I am curious.

share|improve this question
8  
This question on StackOverflow may be enlightening. –  Blrfl Mar 1 at 22:26
    
So history reason. Thank you for the link. Can you write an answer and I put it as resolved ? –  aloisdg Mar 1 at 23:00
    
Links alone don't make good answers, and there isn't a good mechanism for marking tnis question a duplicate of something on another SE site. –  Blrfl Mar 1 at 23:43
1  
So we should open a topic in meta.stackexchange.com or something. I think you should get some karma for the good link and if somebody upvote you, the author from the original answer should get some karma. In fact, the original question should get some karma too. –  aloisdg Mar 1 at 23:46
2  
@aloisdg cross site dups are a old issue on MSO. Chase the linked questions to get a fuller view of it. –  MichaelT Mar 2 at 0:35

3 Answers 3

up vote 51 down vote accepted

In the old days of C, there was no boolean type. People used the int for storing boolean data, and it worked mostly. Zero was false and everything else was true.

This meant if you took an int flag = 0; and later did flag++ the value would be true. This would work no matter what the value of flag was (unless you did it a lot, it rolled over and you got back to zero, but lets ignore that) - incrementing the flag when its value was 1 would give 2, which was still true.

Some people used this for unconditionally setting a boolean value to true. I'm not sure it ever became idiomatic, but its in some code.

This never worked for --, because if the value was anything other than 1 (which it could be), the value would still not be false. And if it was already false (0) and you did a decrement operator on it, it wouldn't remain false.

When moving code from C to C++ in the early days, it was very important that C code included in C++ was still able to work. And so in the specification for C++ (section 5.2.6 (its on page 71)) it reads:

The value obtained by applying a postfix ++ is the value that the operand had before applying the operator. [Note: the value obtained is a copy of the original value ] The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointer to a complete object type. After the result is noted, the value of the 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. ]

The operand of postfix -- is decremented analogously to the postfix ++ operator, except that the operand shall not be of type bool.

This is again mentioned in section 5.3.2 (for the prefix operator - 5.2.6 was on postfix)

As you can see, this is deprecated (Annex D in the document, page 709) and shouldn't be used.

But thats why. And sometimes you may see the code. But don't do it.

share|improve this answer

To partially cope with legacy code which used int or similar as its boolean type.

share|improve this answer
1  
My question is a duplicate from here stackoverflow.com/questions/3450420/… . By the way, thank you for your answer. –  aloisdg Mar 1 at 23:53

To understand the historical significance of this question, you must consider the case of the Therac-25. The Therac-25 was a medical device that delivered radiation to cancer patients. It was plagued with bad programming practices that contributed to its poor safety track record (with multiple deaths attributed to it).

http://courses.cs.vt.edu/professionalism/Therac_25/Therac_1.html

(go to the bottom of page 3)

Every pass through the Set-Up Test routine increments the upper collimator position check, a shared variable called Class3. If Class3 is nonzero, there is an inconsistency and treatment should not proceed. A zero value for Class3 indicates that the relevant parameters are consistent with treatment, and the beam is not inhibited.

...

During machine setup, Set-Up Test will be executed several hundred times since it reschedules itself waiting for other events to occur. In the code, the Class3 variable is incremented by one in each pass through Set-Up Test. Since the Class3 variable is 1 byte, it can only contain a maximum value of 255 decimal. Thus, on every 256th pass through the Set-Up Test code, the variable overflows and has a zero value. That means that on every 256th pass through Set-Up Test, the upper collimator will not be checked and an upper collimator fault will not be detected. The overexposure occurred when the operator hit the "set" button at the precise moment that Class3 rolled over to zero. Thus Chkcol was not executed, and F$mal was not set to indicate the upper collimator was still in field-light position. The software turned on the full 25 MeV without the target in place and without scanning. A highly concentrated electron beam resulted, which was scattered and deflected by the stainless steel mirror that was in the path.

The Therac-25 used something like the equivalent of operator++ on a bool. However, the programming language they used was not C++, and their data type was not bool. Unlike the guarantee in C++, however, a regular integer type simply keeps going up. Their data type was the equivalent of uint8_t.

C++ decided to keep the operator++ around for people used to programming like this, but instead of incrementing the value, it simply sets it to true to prevent things like this.

Note that operator++(bool) is deprecated.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf

Annex D of C++14:

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

share|improve this answer
    
While that would explain why its deprecated, it doesn't explain why it exists in the first place. –  MichaelT Mar 15 at 18:23
    
It exists because there were some people who set a boolean value by incrementing it when programming in C. C++ was designed to ease the transition from C, so they supported it with the bool type. I was just trying to give a historical example of when people actually programmed this way. –  David Stone Mar 15 at 21:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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