From: sbi on
�� Tiib wrote:
> [...]
> 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.

That seemed like a good and valid reason...until I remembered we're talking
streams here. It's input/output. Does the performance of a pointer-to-bool
conversion really matter to test success of reading from or writing to an
external device, including copying of characters (with O(n)!) and probably
even converting other objects to/from string?
That seems like a very dubious 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 28 juuli, 23:48, sbi <non...(a)invalid.invalid> wrote:
> �� Tiib wrote:
> > [...]
> > 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.
>
> That seemed like a good and valid reason...until I remembered we're talking
> streams here. It's input/output. Does the performance of a pointer-to-bool
> conversion really matter to test success of reading from or writing to an
> external device, including copying of characters (with O(n)!) and probably
> even converting other objects to/from string?
> That seems like a very dubious reason.

Templates usually target maximum possible performance, since it is
hard to know when it matters. basic_ios is about stream. stream is
sequence of data coming or going to somewhere.

When stream is for reading/writing very complex to parse data then it
probably gives no effect. When streaming to/from slow external device
then it probably is pointless as well. However when for example two
applications are piped together and use direct streams to interchange
lot of simple to parse data with each other then it may be is saving
cycles?

fail() is anyway there so operator! is not some huge masterpiece ...
it is one-liner synonym declaration.


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

From: sbi on
�� Tiib wrote:
> On 28 juuli, 23:48, sbi <non...(a)invalid.invalid> wrote:
>> �� Tiib wrote:
>>> [...]
>>> 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.
>> That seemed like a good and valid reason...until I remembered we're talking
>> streams here. It's input/output. Does the performance of a pointer-to-bool
>> conversion really matter to test success of reading from or writing to an
>> external device, including copying of characters (with O(n)!) and probably
>> even converting other objects to/from string?
>> That seems like a very dubious reason.
>
> Templates usually target maximum possible performance, since it is
> hard to know when it matters. basic_ios is about stream. stream is
> sequence of data coming or going to somewhere.
>
> When stream is for reading/writing very complex to parse data then it
> probably gives no effect. When streaming to/from slow external device
> then it probably is pointless as well. However when for example two
> applications are piped together and use direct streams to interchange
> lot of simple to parse data with each other then it may be is saving
> cycles?

You got to be kidding. We're talking a pointer-to-bool conversion here
(which is most likely elided and the pointer tested for NULL-ness directly),
compared to the performance of copying a string of characters to/from
somewhere. And that's for the best-case scenarios with no conversions
to/from string and the stream working on memory.
Even in this optimal scenario, the performance of checking a pointer for
NULL-ness compared to checking a boolean for being false certainly doesn't
seem like a valid reason to overload that operator.

> fail() is anyway there so operator! is not some huge masterpiece ...
> it is one-liner synonym declaration.

Yep. An I think that's why it was done. ("...or is it there just because
Jerry Schwarz or whoever added it was playing around with the then brand new
toy of operator overloading?")
Which is all fine, greater sins have been committed in the history of C++
('std::vector<bool>' for starters). But I started out with this question
because I wanted to know whether there is a _technical_ reason.

It seems there is none.

sbi

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

From: James Kanze on
On Jul 13, 2:01 pm, sbi <non...(a)invalid.invalid> 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?

Probably for reasons of orthogonality. Any time you overload
operator bool() (and the operator void*() is a disguised
overload of operator bool()), you also overload operator!().
It's just good coding practice.

--
James Kanze

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

From: sbi on
James Kanze wrote:
> On Jul 13, 2:01 pm, sbi <non...(a)invalid.invalid> wrote:
> [...]
>> Am I missing something here or is it purely for (mistaken) historical
>> reasons that std::basic_ios::operator!() is defined?
>
> Probably for reasons of orthogonality. Any time you overload
> operator bool() (and the operator void*() is a disguised
> overload of operator bool()), you also overload operator!().
> It's just good coding practice.

James, over the years I have learned to respect your knowledge, but "it's
just good coding practice" isn't enough for me to be convinced by you. If
this was true, there ought to be a reason that's possible to explain. Could
you please cite that reason to back up this statements?
Also, there's contrary evidence. For starters, if this was true, the
standard library's smart pointers should follow that practice. Yet, they don't.

> James Kanze

sbi

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