9

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.

  • Wrong, because two succesful calls are not equal.

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.

  • Still wrong however, this doesn't seem to be called at all (and couldn't be, since the two successful calls are still not equal).

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..

  • Shouldn't any operator bool () found be called before trying to casting it to pointer values?
  • Is it some 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?

Community
  • 1
  • 1
default
  • 11,485
  • 9
  • 66
  • 102

1 Answers1

2

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.

cadolphs
  • 9,014
  • 1
  • 24
  • 41
  • Ah, yes.. I see. Thanks, I'm getting tired here I feel. I have a follow up question on this then :) Maybe you can answer [this one](http://stackoverflow.com/q/4294873/238902 "define both operator void* and operator bool") as well..? – default Nov 28 '10 at 01:34
  • However.. The ifstream is actually calling operator void* and not operator!=. – default Nov 28 '10 at 01:40