I am definitely talking about C++
int x = 1;
bool y = x;
# cl.exe /W3 main.c
This code gives a warning on VS2012. But it doesn't give one when the cast is in a loop condition. That is weird.http://stackoverflow.com/a/31552168/5994461
This stackoverflow message talks about the specs for C11, and the first comment adds information on the C++03 spec. It seems that implicit cast from integer to boolean is allowed... under all circumstances... depending on what specification the compiler is following :D
For future references, I'll just summarize this as "C and C++ are minefields". We'll just add that to the list of WTF behaviors.
By the way, if you think that "C has had bool for 19 years" [the C99 spec specifically]. You clearly didn't work in C for long enough with a large variety of tools. The world is bigger than just GCC.
I believe that the justification for that is that you'll often want to do e.g.
while (node) {
node.val += 3;
node = node->next;
}
Implicit conversion of a type into a bool is pretty useful here, or for e.g. while (std::cin >> x >> y) { ... }
It doesn't compile in C++. Conditions have to be booleans. i-- is an integer :D
In C, 'true' is defined as anything else than '0', there is no proper boolean types, i-- is an integer which is perfectly okay for conditions, the condition is equivalent "while (i != 0)".
There might be some variations, errors and warnings depending on the compilers, the strictness level and the revision of the language chosen.