bools...

This is a discussion on bools... within the C++ Programming forums, part of the General Programming Boards category; 3.9.1 Fundamental types 6 Values of type bool are either true or false.*42) [Note: there are no signed, unsigned, short, ...

  1. #16
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    From the C++ Language Standard:

    3.9.1 Fundamental types

    6
    Values of type bool are either true or false.*42) [Note: there are no signed, unsigned, short, or long bool types or values. ] As described below, bool values behave as integral types. Values of type bool participate in integral promotions (4.5).

    *42) Using a bool value in ways described by this International Standard as ‘‘undefined,’’ such as by examining the value of an uninitialized automatic variable, might cause it to behave as if is neither true nor false.


    4.5 Integral promotions

    4
    An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one.

    5 These conversions are called integral promotions.

  2. #17
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    So in closing...bools are true or false. Why would you use a bool for anything more?

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    4.5 Integral promotions

    4 An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one.

    5 These conversions are called integral promotions.
    And the reverse of this is the implicit conversion from pointers, integral or floating point numbers to bool, which states that != 0 becomes true and == 0 becomes false.
    bool b = 100000000;
    is therefore completely legal (though stupid), because the 100 millions are not 0 and thus get implicitely converted to true.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #19
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Geez.


    bool flag=false;

    or

    bool flag=true;


    if (flag)
    {
    dosomethingwhentrue();
    } else dosomethingwhenfalse();

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How get bool's from std::vector<bool>
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2008, 06:24 AM
  2. counting true's and false's from bools
    By quiet_forever in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2007, 03:57 PM
  3. WM_CREATE and BOOLs
    By kidburla in forum Windows Programming
    Replies: 3
    Last Post: 09-19-2006, 12:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21