Implicit convervsion of bool to int: why?

M

MikeP

Why can bool types be implicitly converted to ints? Wouldn't it be better
if a bool was a bool was a bool was a bool...? The complementary question
is also a curiosity: why can an int be converted to a bool? I think the
answer to the last question has to do with "trueness" in C++ historically
meaning "not zero" instead of meaning "one".
 
B

BGB

I was asking WHY they did that (mixed up bools and ints) in C++.

because C++ came directly from C, which generally didn't/doesn't have
bool (a "_Bool" type was added in C99, but is not so often used).

in C tradition, zero means false and non-zero means true, and C++ mostly
just followed suit, making relatively few changes to the fundamental C core.


possibly because in many languages, whether or not zero/non-zero can
represent false/true is a matter of debate, with many languages allowing
this and others rejecting it, so it is not such a clear-cut matter
in-general.


or such...
 
A

Abhishek Padmanabh

because C++ came directly from C, which generally didn't/doesn't have
bool (a "_Bool" type was added in C99, but is not so often used).

in C tradition, zero means false and non-zero means true, and C++ mostly
just followed suit, making relatively few changes to the fundamental C
core.

Except that in C++, the standard streams treat them differently. A
non-zero integral input puts the stream into an error state suggesting
this to be a failure condition. Anywhere else in C++ realms, a non-zero
is true but with streams it is different. Not sure as to why this was
designed so.

Thanks and regards,
Abhishek Padmanabh
 
M

MikeP

Robert said:
Why can shorts be implicitly converted to ints? Or ints to longs? Or
floats to doubles? Or...?

Because the are numeric? (Now what do you think?).
There are languages which have no implicit conversions.

The Haskell, Scheme, OCaml set, yes? Kinda makes me wanna cozy up to
functional languages. (Not ironic that they are called FUNCTIONAL, eh?).
 
B

BGB

Except that in C++, the standard streams treat them differently. A
non-zero integral input puts the stream into an error state suggesting
this to be a failure condition. Anywhere else in C++ realms, a non-zero
is true but with streams it is different. Not sure as to why this was
designed so.

it is not clear what value you are talking about here...

if it is an error status, well, this is not the same as a boolean
true/false. traditionally, an error code of 0 means success.


also, if one has, say:
bool hasError;

well then, one can infer that the bool being true (non-zero, generally
1) means it has an error status, so boolean true/false and
success/failure are not necessarily related.
 
A

Abhishek Padmanabh

it is not clear what value you are talking about here...

I should have said a non-zero integral value input other than 1. For
example:

#include <iostream>
int main ()
{
bool a=false;
std::cout << "insert 0 or 1:"; //insert anything other than 0 or 1
std::cin >> a; //failbit gets set for cin to signify failure
if (a==true)
{
std::cout << "a is true";
}
else
{
std::cout << "a is false";
}
}

Any input value other than 0 or 1 will cause the streams failbit to be
set. However, if I just assigned that value to the boolean, it works
given the implicit conversion rules. Both are not exactly identical
scenarios but it would have been nice if the streams worked with such
inputs too.
 
B

BGB

I should have said a non-zero integral value input other than 1. For
example:

#include <iostream>
int main ()
{
bool a=false;
std::cout << "insert 0 or 1:"; //insert anything other than 0 or 1
std::cin >> a; //failbit gets set for cin to signify failure
if (a==true)
{
std::cout << "a is true";
}
else
{
std::cout << "a is false";
}
}

the example above points out what was being talked about, but your
wording was very non-obvious.

Any input value other than 0 or 1 will cause the streams failbit to be
set. However, if I just assigned that value to the boolean, it works
given the implicit conversion rules. Both are not exactly identical
scenarios but it would have been nice if the streams worked with such
inputs too.


errm, this is a matter of input validation. in the case of a non-0/1
answer, iostream just doesn't want to accept it (for whatever reason),
although sanely it could have accepted other values and coerced them, or
even accepted other bool-like tokens (yes/no, true/false, ...).

but, this is iostream-specific behavior.


personally though, I don't much use iostream (I more prefer stdio and
doing parsing more manually instead).
 
M

MikeP

Robert said:
I think from a C perspective, bools are very close to numerics. You
may think otherwise.

Outside of the C perversion, in general they are logicals. A 2-state type
does not seem very numeric to me.
 
J

James Kanze

Why can bool types be implicitly converted to ints? Wouldn't it be better
if a bool was a bool was a bool was a bool...? The complementary question
is also a curiosity: why can an int be converted to a bool? I think the
answer to the last question has to do with "trueness" in C++ historically
meaning "not zero" instead of meaning "one".

The reasons are purely historical. B only had a single type,
word, which was used for everything. When used in a boolean
context, the word was interpreted as an int (even if it was in
fact a pointer or a floating point), and compared with 0. When
C added types, the original motivation was to distinguish
between different sized values, and to allow operator
overloading (same operator for integral addition and floating
point addition). Bool wasn't necessary for that, so it didn't
get added. And C++ originally just adopted C's rules. When
bool was added to C++, it was intentionally done in a way that
would allow the simplest conversions for the enormous number of
programs using typedef's and #define's to simulate bool's, and
in a way which wouldn't break existing code. (IIRC, the original
proposal did recommend deprecating the conversions. But that
somehow got lost in the final proposal.)
 
N

Niklas Holsti

MikeP said:
The Haskell, Scheme, OCaml set, yes?

For an example of a language without implicit conversions, from the same
programming paradigm as C++ (imperative, procedural), take Ada.
 
H

HonestAbe

Niklas said:
For an example of a language without implicit conversions, from the
same programming paradigm as C++ (imperative, procedural), take Ada.

"Take Ada" and... what? Why don't YOU take Ada, and I'll take the hot
chick!. :p

(I know Ada doesn't do implicit conversions. Not intimately, but I
know/knes that). I am the choir, don't preach to me, or I'll belt out a
high note and bust your eardrum. So remember, you are a just a priest,
not god.
 
N

Niklas Holsti

HonestAbe said:
"Take Ada" and... what?

Did you have trouble with my grammar? MikeP was asking (indirectly,
perhaps) for examples of languages without implicit conversions. So I
said, "take Ada as an example". Clear now?
Why don't YOU take Ada, and I'll take the hot chick!. :p

I already took Ada. Since about 1985 it still is the language that fits
me best, thanks for asking.

I trust you and the hot chick are having a good time, too.
 
J

JBarleycorn

Niklas said:
Did you have trouble with my grammar? MikeP was asking (indirectly,
perhaps) for examples of languages without implicit conversions. So I
said, "take Ada as an example". Clear now?

I remember when I was just boy (like you?). Knee-high to my daddy... you
know what he said to me? "don't let the man get ya"
I already took Ada. Since about 1985 it still is the language that
fits me best, thanks for asking.

I trust you and the hot chick are having a good time, too.

Don't offer your wife to me unless you suck\

[I do know your wife, that is why I'm here drinking... I missed the
opportunity, HUH ... damn, I have no resolve.. I don't love your wife,
she reminded me of , why I hate you)

Asshole.
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top