Why is iostream converted to void* ?

H

hpsMouse

Hello,
I just met something that confused me.

An iostream can be converted to a void*, indicating whether the stream
failed.
But since it only acts as a bool type, why not just directly converted
to bool?
 
M

ManicQin

Oh... But who will write that?
I'm sure I will never think of using "+" with a stream...

I'm pretty sure I would do that at least once (not that I ever did).
But after I the first time that

cout << 10;
int i = cout + 3;

and I wont be 13 as expected (at least by me) I wouldn't try it again.
 
J

Jorgen Grahn

Oh... But who will write that?
I'm sure I will never think of using "+" with a stream...

That belongs in the "famous last words" class of statements.

Here the designers had a choice between allowing conversions which
made sense, and to add a larger set which did not make sense. C++
programmers generally agree I think that the type system should not
allow those kinds of errors.

I'm not sure why Pete wrote "early paranoia" ... as far as I know
people still avoid defining conversions to bool, precisely because it
opens up for conversion to all kinds of integer types -- conversions
which usually do not make sense.

/Jorgen
 
P

Paul N

Hello,
I just met something that confused me.

An iostream can be converted to a void*, indicating whether the stream
failed.
But since it only acts as a bool type, why not just directly converted
to bool?

I think the worry was about the << and >> operators. For instance, you
want:

str << 5;

to send the value 5 to the stream str, but if str can be converted to
a bool, which can be converted to an int, it might try working out 1
<< 5, which is 32, without sending anything anywhere.
 
K

Keith H Duggar

That belongs in the "famous last words" class of statements.

Here the designers had a choice between allowing conversions which
made sense, and to add a larger set which did not make sense. C++
programmers generally agree I think that the type system should not
allow those kinds of errors.

I'm not sure why Pete wrote "early paranoia" ... as far as I know
people still avoid defining conversions to bool, precisely because it
opens up for conversion to all kinds of integer types -- conversions
which usually do not make sense.

Agreed. Hence idioms such as SafeBool which many have adopted.
For example you will see SafeBool throughout Boost.

KHD
 
P

Paul N

The rules for overload resolution say that converting the first operand
to a reference in order to call the stream inserter beats any other
conversions; in particular, the user-defined conversion to bool always
loses to the object-to-reference conversion.

Ah. Thanks very much for the explanation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top