Topic: type bool values?


Author: hopps@mmm.com (Kevin J. Hopps)
Date: 21 Apr 1994 15:39:36 GMT
Raw View
What happens when values other than true and false are stored in a bool?

Are there requirements as to sizeof(bool)?

What does the following code print?

    bool x = true;
    x++;
    bool y = x;
    bool z = 65536;

    cout << x << end;
    cout << y << end;
    cout << z << end;
    cout << (x == true) << endl;
    cout << (y == z) << endl;




Author: dag@control.lth.se (Dag Bruck)
Date: 22 Apr 1994 10:43:30 GMT
Raw View
>>>>> "K" == Kevin J Hopps <hopps@mmm.com> writes:

K> What happens when values other than true and false are stored in a
K> bool?  Are there requirements as to sizeof(bool)?

Zero becomes false and non-zero values become true. sizeof(bool) >= 1,
but bitfields of type bool are also ok.

K>     bool x = true; x++; bool y = x; bool z = 65536;

They are all true and compare equal.
What is actually printed has not yet been finally decided.

    -- Dag




Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Fri, 22 Apr 1994 16:39:06 GMT
Raw View
hopps@mmm.com (Kevin J. Hopps) writes:

>What happens when values other than true and false are stored in a bool?

To be pedantic, this can never happen.  Values of types other than
bool will first be converted to bool before they are stored in a bool.
The only values of type bool are `true' and `false'.

To answer your next question, "what happens when values are converted to
bool?", the answer is that the value is compared with 0; if the value
is equal to 0, the converted value is `false', and if the value compares
unequal to 0, then the converted value is `true'.

>Are there requirements as to sizeof(bool)?

Yes, sizeof(bool) >= 1.

>What does the following code print?

This has not yet been decided, since cout << bool has not yet
been decided on.  I'll assume that it outputs "true" or "false".

>    bool x = true;
>    x++;
>    bool y = x;
>    bool z = 65536;
>
>    cout << x << end;

true

>    cout << y << end;

true

>    cout << z << end;

true

>    cout << (x == true) << endl;

true

>    cout << (y == z) << endl;

true

--
Fergus Henderson - fjh@munta.cs.mu.oz.au




Author: kanze@us-es.sel.de (James Kanze)
Date: 22 Apr 1994 20:16:58 GMT
Raw View
In article <2p66no$5nn@dawn.mmm.com> hopps@mmm.com (Kevin J. Hopps)
writes:

|> What happens when values other than true and false are stored in a bool?

Hopefully, you'll get a warning from the compiler.

According to the proposal adapted, the value will be compared to zero,
and the results of the comparison will be stored.

|> Are there requirements as to sizeof(bool)?

>= 1.  (Ie: it must be an addressable type.  An implementation cannot
pack several bool's on a char, for example.)

|> What does the following code print?

|>     bool x = true;
|>     x++;
|>     bool y = x;
|>     bool z = 65536;

|>     cout << x << end;
|>     cout << y << end;
|>     cout << z << end;
|>     cout << (x == true) << endl;
|>     cout << (y == z) << endl;


I will suppose that the bools being output are cast to int's.  There
is a proposal to overload operator<< for bool, but I haven't read it
yet.  Under these conditions (and with endl instead of end in the
first three lines):

 1
 1
 1
 1
 1
--
James Kanze                       email: kanze@lts.sel.alcatel.de
GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                   -- Beratung in industrieller Datenverarbeitung