Whoops, my first version of this post was ENTIRELY WRONG -- very embarrasing -- and this is a complete rewrite;
I had to go back to the books to clarify how the negation operator (“!”) works and the following is a better discussion of the topic;
First, you can apply the negation operator "!" to ANY "arithmetic type" - which includes boolean, integer and float values.
BUT it only returns a boolean value of “1” or “0”
So, thinking about “!”how it treats each arithmetic type;
Boolean: either "1" or "0" and "!" simply reverses them.
For a non-zero integer or float, "!int" (or "!float") evaluates to "0" otherwise "!0" evaluates to "1" - so...
!a is valid when a is of boolean, integer or float type.
-- If a = 0 for boolean, int or float then !a = boolean value of 1
-- If a = 1 for boolean then !a = boolean value of 0
-- If a > 0 for int or float then !a = boolean value of 0
The following code demonstrates the above;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
float test = 0;
for (int count = 0; count < 10; count++)
{
cout << "\ncount = " << count << " !count = " << !count;
test = count;
cout << "\t\ttest = " << test << " !test = " << !test;
}
cout << "\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
|
SO - to answer your original question...
if int i=5, then;
(!a++) is "false" because;
a++ gives an integer value of 6, and !6 gives a boolean value of 0
why ((!a)++) or (++(!a)) gives an error is trickier, but the responses my compiler gave to the following suggest something consistent is happening;
1 2 3 4 5 6
|
bool a = "0";
cout << a; // this will work
cout << !a; // this will work
cout << a++; // this will work
cout << !a++; // even this will work
cout << (!a)++; // but this will give the error "non l-value in increment"
|
I don't know what is happening here, but it seems an intriguing question. I'll see if I can find an answer...