Topic: operator void* precedence


Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/04/20
Raw View
In article B475FB13@ts-i.com, Thomas Harris <tharris@ts-i.com> writes:
>I'm a little confused about what seemed like a simple expression
>
>(foo == bar)
>
>given
>iostream foo, bar;
>
>This is false in msvc++ and true in g++! In g++ it behaves like
>
>(foo.fail() == bar.fail())
>
>It appears that, in g++, operator void* takes presedence over
>operator==.
>
>... Anybody know what's really going on, and what
>the draft standard has to say about this?

There is no operator== defined for streams, and it isn't obvious
to me what the semantics should be if you defined one of your own.
Same stream? Same streambuf? Contents of streams are the same from
offset 0 to eof? Contents from current positions to the end the same?
Streams have the same error state? Streams have the same formatting
state? All of the above?

The presence of operator void* (which serves the purpose of an operator
bool) means that instead of the code failing to compile, each stream
gets converted to void*, and the pointers are compared.

The definition of operator void* is that it returns a null pointer
if the stream is in a failed state or worse, and a non-null pointer
otherwise. The value of the non-null pointer returned is up to the
implementation. Some implementations return the address of the stream
for the non-null pointer, and some return a constant value.

If neither stream has failed, the comparison could yield either a true
or false result. The problem is not with the implementations, but
with trying to compare streams.

I can't suggest alternative code, because I don't know what condition
you want to test.

---
Steve Clamage, stephen.clamage@sun.com



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Thomas Harris <tharris@ts-i.com>
Date: 1998/04/16
Raw View
I'm a little confused about what seemed like a simple expression

(foo == bar)

given
iostream foo, bar;

This is false in msvc++ and true in g++! In g++ it behaves like

(foo.fail() == bar.fail())

It appears that, in g++, operator void* takes presedence over
operator==.

void* iostream::operator void*(iostream& blah) {
  return (blah.fail()? (void*)0: (void*)1);
}

Usefull in

while (blah) //same as while (!blah.fail())

This makes sense to me, actually, but I'm not sure if that's really
what's happening or not. Anybody know what's really going on, and what
the draft standard has to say about this? If the g++ version is correct,
how does one compare iostreams? I've been doing

(foo.rdbuf() == bar.rdbuf())

as a temporary measure. If the msvc++ version is ultimately correct, is
there a way to fix g++?

-tk


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]