From: sbi on
Hallo,

The C++ IO streams' base class std::basic_ios defines operator void*()
to return a safe bool indicating !fail() and operator!() to return fail().
That makes me wonder why we need the operator!() at all. Certainly, !is
would also work by implicitly calling operator void*() and negating its
result.

Am I missing something here or is it purely for (mistaken) historical
reasons that std::basic_ios::operator!() is defined?

Thanks,

sbi

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Francis Glassborow on
sbi wrote:
> Hallo,
>
> The C++ IO streams' base class std::basic_ios defines operator void*()
> to return a safe bool indicating !fail() and operator!() to return fail().
> That makes me wonder why we need the operator!() at all. Certainly, !is
> would also work by implicitly calling operator void*() and negating its
> result.
>
> Am I missing something here or is it purely for (mistaken) historical
> reasons that std::basic_ios::operator!() is defined?
>
I suspect that you are being confused by the need to sidestep issues
with bool.

For a brief time in the 90s (4 months to be exact) the then working
paper for C++ supplied an operator bool for IO streams. Unfortunately
the horrible compromise made to get bool into the language as a
fundamental type returned by such things as comparison operators meant
that with that specification such things as:

std::cin << x;

became valid.
The fix was to provide operator void* instead (a null pointer would then
be equivalent to false, but applying such things as shift operators
would result in compile time errors)

operator! has been in the specification for as long as I can remember
and leaving it alone seems harmless (if it isn't broke do not fix it,
that way it will certainly stay unbroken.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: sbi on
Francis Glassborow wrote:
> sbi wrote:
>> Hallo,
>>
>> The C++ IO streams' base class std::basic_ios defines operator void*()
>> to return a safe bool indicating !fail() and operator!() to return fail().
>> That makes me wonder why we need the operator!() at all. Certainly, !is
>> would also work by implicitly calling operator void*() and negating its
>> result.
>>
>> Am I missing something here or is it purely for (mistaken) historical
>> reasons that std::basic_ios::operator!() is defined?
>>
> I suspect that you are being confused by the need to sidestep issues
> with bool.
> [...]

No, I am not, although it seems my posting confused you. I know what
operator void*() does and I know why it isn't operator bool(). (Note that I
wrote "safe bool". <http://www.artima.com/cppsource/safebool.html>) I must
have learned this about 15 years ago.

> operator! has been in the specification for as long as I can remember
> and leaving it alone seems harmless (if it isn't broke do not fix it,
> that way it will certainly stay unbroken.

And what I want to know is: Why do we have operator!() in the first place?
Is there a _technical_ reason I'm missing or is it there just because Jerry
Schwarz or whoever added it was playing around with the then brand new toy
of operator overloading? I am just asking because I can't see a technical
reason. I am perfectly fine either way. I just want to know.

sbi

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: sbi on
sbi wrote:
> [...]
>>> The C++ IO streams' base class std::basic_ios defines operator void*()
>>> to return a safe bool indicating !fail() and operator!() to return fail().
>>> That makes me wonder why we need the operator!() at all. Certainly, !is
>>> would also work by implicitly calling operator void*() and negating its
>>> result.
>>>
>>> Am I missing something here or is it purely for (mistaken) historical
>>> reasons that std::basic_ios::operator!() is defined?
>
> [...] what I want to know is: Why do we have operator!() in the first place?
> Is there a _technical_ reason I'm missing or is it there just because Jerry
> Schwarz or whoever added it was playing around with the then brand new toy
> of operator overloading? I am just asking because I can't see a technical
> reason. I am perfectly fine either way. I just want to know.

I suppose the fact that nobody knows how to answer this can be taken as an
indication that there is indeed no technical reason?

sbi

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Öö Tiib on
On 23 juuli, 03:00, sbi <non...(a)invalid.invalid> wrote:
> sbi wrote:
> > [...]
> >>> The C++ IO streams' base class std::basic_ios defines operator void*()
> >>> to return a safe bool indicating !fail() and operator!() to return fail().
> >>> That makes me wonder why we need the operator!() at all. Certainly, !is
> >>> would also work by implicitly calling operator void*() and negating its
> >>> result.
>
> >>> Am I missing something here or is it purely for (mistaken) historical
> >>> reasons that std::basic_ios::operator!() is defined?
>
> > [...] what I want to know is: Why do we have operator!() in the first place?
> > Is there a _technical_ reason I'm missing or is it there just because Jerry
> > Schwarz or whoever added it was playing around with the then brand new toy
> > of operator overloading? I am just asking because I can't see a technical
> > reason. I am perfectly fine either way. I just want to know.
>
> I suppose the fact that nobody knows how to answer this can be taken as an
> indication that there is indeed no technical reason?

For operator void* there are no other reasons but for to allow more
convenient syntax for detection if there were no failures. Instead of
using "if ( !some_basic_ios.fail() )" or "if ( !!some_basic_ios )" you
may use "if ( some_basic_ios )".

For operator! on the other hand the reason is probably efficiency. Why
to have a pointer converted to bool and then inbuilt operator!()
applied? I have seen bit-wise logic operators used as reasonable
performance optimization to get rid of slowness of something-bool
conversions.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]