Implicit convervsion of bool to int: why?

Discussion in 'C++' started by MikeP, Jul 16, 2011.

  1. MikeP

    MikeP Guest

    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".
     
    MikeP, Jul 16, 2011
    #1
    1. Advertising

  2. MikeP

    Krice Guest

    On 16 heinä, 12:29, "MikeP" <> wrote:
    > Wouldn't it be better
    > if a bool was a bool was a bool was a bool...?


    Yes, it's better. Don't mix bools and ints, they are different types.
     
    Krice, Jul 16, 2011
    #2
    1. Advertising

  3. MikeP

    MikeP Guest

    Krice wrote:
    > On 16 heinä, 12:29, "MikeP" <> wrote:
    >> Wouldn't it be better
    >> if a bool was a bool was a bool was a bool...?

    >
    > Yes, it's better. Don't mix bools and ints, they are different types.


    I was asking WHY they did that (mixed up bools and ints) in C++.
     
    MikeP, Jul 16, 2011
    #3
  4. MikeP

    BGB Guest

    On 7/16/2011 2:43 AM, MikeP wrote:
    > Krice wrote:
    >> On 16 heinä, 12:29, "MikeP"<> wrote:
    >>> Wouldn't it be better
    >>> if a bool was a bool was a bool was a bool...?

    >>
    >> Yes, it's better. Don't mix bools and ints, they are different types.

    >
    > 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...
     
    BGB, Jul 16, 2011
    #4
  5. On 7/16/2011 10:54 AM, BGB wrote:
    > On 7/16/2011 2:43 AM, MikeP wrote:
    >> Krice wrote:
    >>> On 16 heinä, 12:29, "MikeP"<> wrote:
    >>>> Wouldn't it be better
    >>>> if a bool was a bool was a bool was a bool...?
    >>>
    >>> Yes, it's better. Don't mix bools and ints, they are different types.

    >>
    >> 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.


    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
     
    Abhishek Padmanabh, Jul 16, 2011
    #5
  6. MikeP

    MikeP Guest

    Robert Wessel wrote:
    > On Sat, 16 Jul 2011 04:29:14 -0500, "MikeP" <> wrote:
    >
    >> 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".

    >
    >
    > 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?).

    > There's an
    > argument that it's safer, although it certainly leads to a lot of
    > extra typing. But a number of implicit conversion ("promotions") have
    > always been part of C - the ones for bool are not really out of line
    > given the usual way truth has been defined in C.
     
    MikeP, Jul 17, 2011
    #6
  7. MikeP

    BGB Guest

    On 7/16/2011 10:52 AM, Abhishek Padmanabh wrote:
    > On 7/16/2011 10:54 AM, BGB wrote:
    >> On 7/16/2011 2:43 AM, MikeP wrote:
    >>> Krice wrote:
    >>>> On 16 heinä, 12:29, "MikeP"<> wrote:
    >>>>> Wouldn't it be better
    >>>>> if a bool was a bool was a bool was a bool...?
    >>>>
    >>>> Yes, it's better. Don't mix bools and ints, they are different types.
    >>>
    >>> 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.

    >
    > 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.
     
    BGB, Jul 17, 2011
    #7
  8. On 7/17/2011 7:03 AM, BGB wrote:
    > On 7/16/2011 10:52 AM, Abhishek Padmanabh wrote:
    >> On 7/16/2011 10:54 AM, BGB wrote:
    >>> On 7/16/2011 2:43 AM, MikeP wrote:
    >>>> Krice wrote:
    >>>>> On 16 heinä, 12:29, "MikeP"<> wrote:
    >>>>>> Wouldn't it be better
    >>>>>> if a bool was a bool was a bool was a bool...?
    >>>>>
    >>>>> Yes, it's better. Don't mix bools and ints, they are different types.
    >>>>
    >>>> 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.

    >>
    >> 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...
    >


    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.
     
    Abhishek Padmanabh, Jul 17, 2011
    #8
  9. MikeP

    BGB Guest

    On 7/17/2011 1:02 AM, Abhishek Padmanabh wrote:
    > On 7/17/2011 7:03 AM, BGB wrote:
    >> On 7/16/2011 10:52 AM, Abhishek Padmanabh wrote:
    >>> On 7/16/2011 10:54 AM, BGB wrote:
    >>>> On 7/16/2011 2:43 AM, MikeP wrote:
    >>>>> Krice wrote:
    >>>>>> On 16 heinä, 12:29, "MikeP"<> wrote:
    >>>>>>> Wouldn't it be better
    >>>>>>> if a bool was a bool was a bool was a bool...?
    >>>>>>
    >>>>>> Yes, it's better. Don't mix bools and ints, they are different types.
    >>>>>
    >>>>> 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.
    >>>
    >>> 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...
    >>

    >
    > 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).
     
    BGB, Jul 17, 2011
    #9
  10. MikeP

    MikeP Guest

    Robert Wessel wrote:
    > On Sun, 17 Jul 2011 00:02:02 -0500, "MikeP" <> wrote:
    >
    >> Robert Wessel wrote:
    >>> On Sat, 16 Jul 2011 04:29:14 -0500, "MikeP" <>
    >>> wrote:
    >>>
    >>>> 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".
    >>>
    >>>
    >>> 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?).

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


    I hate you.
     
    MikeP, Jul 17, 2011
    #10
  11. MikeP

    MikeP Guest

    Robert Wessel wrote:
    > On Sun, 17 Jul 2011 00:02:02 -0500, "MikeP" <> wrote:
    >
    >> Robert Wessel wrote:
    >>> On Sat, 16 Jul 2011 04:29:14 -0500, "MikeP" <>
    >>> wrote:
    >>>
    >>>> 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".
    >>>
    >>>
    >>> 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?).

    >
    >
    > 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.
     
    MikeP, Jul 20, 2011
    #11
  12. MikeP

    James Kanze Guest

    On Jul 16, 10:29 am, "MikeP" <> wrote:
    > 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.)

    --
    James Kanze
     
    James Kanze, Jul 20, 2011
    #12
  13. MikeP wrote:
    > Robert Wessel wrote:
    >> On Sat, 16 Jul 2011 04:29:14 -0500, "MikeP" <> wrote:
    >>
    >>> Why can bool types be implicitly converted to ints?

    ...
    >> There are languages which have no implicit conversions.

    >
    > 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.

    --
    Niklas Holsti
    Tidorum Ltd
    niklas holsti tidorum fi
    . @ .
     
    Niklas Holsti, Jul 30, 2011
    #13
  14. MikeP

    HonestAbe Guest

    Niklas Holsti wrote:
    > MikeP wrote:
    >> Robert Wessel wrote:
    >>> On Sat, 16 Jul 2011 04:29:14 -0500, "MikeP" <>
    >>> wrote:
    >>>> Why can bool types be implicitly converted to ints?

    > ...
    >>> There are languages which have no implicit conversions.

    >>
    >> 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.


    "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.
     
    HonestAbe, Jul 31, 2011
    #14
  15. HonestAbe wrote:
    > Niklas Holsti wrote:
    >> MikeP wrote:
    >>> Robert Wessel wrote:
    >>>> On Sat, 16 Jul 2011 04:29:14 -0500, "MikeP" <>
    >>>> wrote:
    >>>>> Why can bool types be implicitly converted to ints?

    >> ...
    >>>> There are languages which have no implicit conversions.
    >>> 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.

    >
    > "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.

    --
    Niklas Holsti
    Tidorum Ltd
    niklas holsti tidorum fi
    . @ .
     
    Niklas Holsti, Jul 31, 2011
    #15
  16. MikeP

    JBarleycorn Guest

    Niklas Holsti wrote:
    > HonestAbe wrote:
    >> Niklas Holsti wrote:
    >>> MikeP wrote:
    >>>> Robert Wessel wrote:
    >>>>> On Sat, 16 Jul 2011 04:29:14 -0500, "MikeP" <>
    >>>>> wrote:
    >>>>>> Why can bool types be implicitly converted to ints?
    >>> ...
    >>>>> There are languages which have no implicit conversions.
    >>>> 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.

    >>
    >> "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?


    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"

    >
    >> 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.


    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.
     
    JBarleycorn, Jul 31, 2011
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,223
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,650
    Old Wolf
    Jan 20, 2004
  3. Alexander Korovyev

    implicit long int -> int ?

    Alexander Korovyev, May 10, 2004, in forum: C Programming
    Replies:
    1
    Views:
    319
    Jeremy Yallop
    May 10, 2004
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,003
    Smokey Grindel
    Dec 2, 2006
  5. Replies:
    2
    Views:
    421
    Alf P. Steinbach
    Dec 3, 2005
Loading...

Share This Page