Conversion of char * to bool

Discussion in 'C++' started by Christopher Benson-Manica, Jan 13, 2006.

  1. I have compiled the following program with two different
    implementations without any warnings at all. Why is a char *
    converted to a bool (apparently) without a cast?

    void foo( bool a ) {}

    int main() {
    foo( "123" );
    const char *bar="123";
    foo( bar );
    return 0;
    }

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jan 13, 2006
    #1
    1. Advertising

  2. Christopher Benson-Manica

    TB Guest

    Christopher Benson-Manica sade:
    > I have compiled the following program with two different
    > implementations without any warnings at all. Why is a char *
    > converted to a bool (apparently) without a cast?
    >


    Because the C++ Standard allows it. It's convenient.
    A zero arithmetic value or null pointer value is converted to false,
    everything else to true.

    > void foo( bool a ) {}
    >
    > int main() {
    > foo( "123" );
    > const char *bar="123";
    > foo( bar );
    > return 0;
    > }
    >


    TB
     
    TB, Jan 13, 2006
    #2
    1. Advertising

  3. TB <> wrote:

    > Because the C++ Standard allows it. It's convenient.


    I disagree wholeheartedly WRT the "convenience" of this behavior. I
    spent some time tracking down some bizarre behavior that turned out to
    be caused by a change in the prototype of a function from

    void foo( const char *, const char * );

    to

    void foo( const char *, bool, const char * );

    > A zero arithmetic value or null pointer value is converted to false,
    > everything else to true.


    What would have been so complicated about

    foo( "foo", myCharPtr != NULL );

    ?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jan 14, 2006
    #3
  4. Christopher Benson-Manica

    Bo Persson Guest

    "Christopher Benson-Manica" <> skrev i
    meddelandet news:dq9jel$q0k$...
    > TB <> wrote:
    >
    >> Because the C++ Standard allows it. It's convenient.

    >
    > I disagree wholeheartedly WRT the "convenience" of this behavior.


    All too true!

    The convenience comes for old C from before the appearance of the bool
    type. Traditionally any expression could be used as a condition value,
    treating zero as false and non-zero as true.

    Unfortunately, there is way too much old code out there, for anyone to
    dare to change this rule.


    Bo Persson
     
    Bo Persson, Jan 14, 2006
    #4
    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. Alexandros

    char* to vector<bool>

    Alexandros, Dec 27, 2003, in forum: C++
    Replies:
    3
    Views:
    759
    Martijn Lievaart
    Dec 30, 2003
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,149
    Ian Collins
    May 9, 2006
  3. Replies:
    2
    Views:
    438
    Alf P. Steinbach
    Dec 3, 2005
  4. Travis

    char * to bool

    Travis, Jul 13, 2007, in forum: C++
    Replies:
    6
    Views:
    4,540
    Zachary Turner
    Jul 13, 2007
  5. Florian Kaufmann
    Replies:
    1
    Views:
    488
    Victor Bazarov
    Feb 17, 2012
Loading...

Share This Page