I am implementing boolean operator overloads and have some strange results for unary operators. My code shows that in gcc 4.5.3:
1 2 3 4 5 6 7 8
actual results truefalse
!true = !false == 0 0 1 (logical not of value)
~true = ~false == -2 0xFE 0xFF (binary not of value)
+true = +false == 1 (depends ifbool is an int or bool)
++true = ++false == 1 (ifbool then illegal otherwise ++int)
true++ = false++ == 1 (same as above)
--true = -- false == NOT ALLOWED (legal and treating like bool++)
Where some values depend on whether the boolean input is treated as a bool (with values true|false), or an int (with values 0|1). If it treated as a boolean then I would expect the results to be a boolean (true|false) or within the range of a boolean treated as an int (0|1).
The results are unexpected and appear wrong. Is my code wrong, gcc at fault, or my understanding faulty? Any help is appreciated.
Your expectations appear to be different from how C++ defines the results of those operations, but your code is also very wrong: your func0() output statement invokes undefined behavior because it attempts to modify (via opeator++) and access (many times) the same object without any sequencing.
Note that ++ for bools is a bizarre anachronism, it was deprecated the moment it was inherited from C, and will likely be removed from the language soon.
I don't understand your comments on func0() having undefined output behavior. Could you elaborate?
When I coded I thought I'd done just fine (sigh). Except of X++ and ++X, each statement presents the result of a computation without altering the value of the variable, and I assumed the variable was then immediately available for a subsequent operation. I don't see the issue.