When answering this question I made some research which really confuses me.
I noticed that two ifstreams that succesfully open are not equal but two ifstreams that fail are.
At first i checked cplusplus.com. The operator !
returns the status of the badbit and failbit. I think that the opposite of this would still be to return the status of these two bits, but flipped.
So then I figured it was an operator bool
somewhere that would return something. So I tried to backtrack from ifstream and found the istream::operator bool()
, which is returning _Ok
.
So I changed my approach and checked the disassembly from Visual Studio. And what do I find?if (file0 != file1) {
doesn't call the operator bool()
, but rather the operator void* ()
(or really __imp_std::ios_base::operator void *
).
So the questions I have are..
operator bool ()
found be called before trying to casting it to pointer values?operator bool()
I missed that in turn is calling the operator void*
?Is this some optimizing that I don't understand?
Or am I completely wrong in that C++ actually thinks that void*
is a better match than bool
in this comparison?
The following table (taken from cppreference.com) shows the precedence of C++ operators. Precedence Level 1 signifies operators of highest priority, while Precedence Level 17 signifies operators of the lowest priority.
The precedence of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3 , the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator. Parentheses may be used to force precedence, if necessary.
Operator precedence specifies the order of operations in expressions that contain more than one operator. Operator associativity specifies whether, in an expression that contains multiple operators with the same precedence, an operand is grouped with the one on its left or the one on its right.
Explanation: Operator ++ has the highest precedence than / , * and +.
1.) You cannot overload multiple times but with different return types.
2.) Yes, operator!
returns the badbit/failbit, but operator!
is something entirely different from operator!=
, which is the one you are using.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With