Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bool operator ++ and --

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 just a random decision, or there is some reason behind this?

This compiles:

static HMODULE hMod = NULL; static bool once = false; if (!once++)     hMod = LoadLibrary("xxx"); 

This does not:

static HMODULE hMod = NULL; static bool once = true; if (once--)     hMod = LoadLibrary("xxx"); 
like image 758
Suma Avatar asked Aug 10 '10 15:08

Suma


People also ask

Can you ++ a bool?

++ is allowed on bools for compatibility with this, but its use is deprecated in the standard and it was removed in C++17.

What is bool operator in C++?

In C++, the data type bool has been introduced to hold a boolean value, true or false. The values true or false have been added as keywords in the C++ language.

Is bool always 0 or 1?

Boolean values and operations C++ is different from Java in that type bool is actually equivalent to type int. Constant true is 1 and constant false is 0. It is considered good practice, though, to write true and false in your program for boolean values rather than 1 and 0.

What does |= mean in C++?

|= just assigns the bitwise OR of a variable with another to the one on the LHS.


1 Answers

It comes from the history of using integer values as booleans.

If x is an int, but I am using it as a boolean as per if(x)... then incrementing will mean that whatever its truth value before the operation, it will have a truth-value of true after it (barring overflow).

However, it's impossible to predict the result of -- given knowledge only of the truth value of x, as it could result in false (if the integral value is 1) or true (if the integral value is anything else - notably this includes 0 [false] and 2 or more [true]).

So as a short-hand ++ worked, and -- didn't.

++ is allowed on bools for compatibility with this, but its use is deprecated in the standard and it was removed in C++17.


This assumes that I only use x as an boolean, meaning that overflow can't happen until I've done ++ often enough to cause an overflow on it's own. Even with char as the type used and CHAR_BITS something low like 5, that's 32 times before this doesn't work any more (that's still argument enough for it being a bad practice, I'm not defending the practice, just explaining why it works) for a 32-bit int we of course would have to use ++ 2^32 times before this is an issue. With -- though it will only result in false if I started with a value of 1 for true, or started with 0 and used ++ precisely once before.

This is different if we start with a value that is just a few below 0. Indeed, in such a case we might want ++ to result in the false value eventually such as in:

int x = -5; while(++x)   doSomething(x); 

However, this example treats x as an int everywhere except the conditional, so it's equivalent to:

int x = -5; while(++x != 0)   doSomething(x); 

Which is different to only using x as a boolean.

like image 136
Jon Hanna Avatar answered Sep 20 '22 11:09

Jon Hanna