Topic: Boolean type: Limited usefullness


Author: kevlin@wslint.demon.co.uk (Kevlin Henney)
Date: Fri, 25 Nov 1994 11:46:24 +0000
Raw View
In article <1994Nov21.131049.23977@leeds.ac.uk>
           sp106@ebor.york.ac.uk "Stephen Parker" writes:

>Kevlin Henney (kevlin@wslint.demon.co.uk) wrote:
>: In article <1994Nov18.114246.6532@leeds.ac.uk>
>:            sp106@ebor.york.ac.uk "Stephen Parker" writes:
>
>: >Hans Walheim (hans@iar.se) wrote:
>: >I already have a boolean class that performs
>: >as well as the current language specification allows, a standardised version
>: >of this is a help but does not answer the problem I have describled.
>
>: It is not possible to write a bool class that behaves like a proper Boolean -
>: many have tried, all have failed. This is one reason (among many) that bool
>: has been accepted as a fundamental type, ie. it satisfies all of your
> queries.
>
>In what way is it not possible to write a proper Boolean? I would like to
>think that my 'Boolean' type performs acceptably -- not withstanding the
>problem that has been discussed of most c++ imnplementations of the if(...)
>condition currently not accepting a boolean type ( I think this change is one
>of the most welcome ).  What other problems are there?

If you wish to preserve the expected behaviour of the && and || operators,
you have to use the ones that expect ints and, more importantly, yield
ints. Consider:

 class MyBool {...};
 void fn(int);
 void fn(MyBool);
 ...
 MyBool p, q;
 ...
 fn(p && q);

This will calls fn(int) and not fn(MyBool). This is one of the many problems
you will encounter, and is one of the reasons that it is impossible to
write a Boolean class that behaves correctly in all situations (ie. as a
Boolean and not an int).

Hope this helps.

+---------------------------+-------------------------------------------+
| Kevlin A P Henney         | Human vs Machine Intelligence:            |
| kevlin@wslint.demon.co.uk | Humans can wreck a nice beach more easily |
| Westinghouse Systems Ltd  |                                           |
+---------------------------+-------------------------------------------+




Author: dag@control.lth.se (Dag Bruck)
Date: 18 Nov 1994 19:54:13 GMT
Raw View
>>>>> "H" == Hans Walheim <hans@iar.se> writes:

H> You may also run into problems with integer-promotion (if
H> sizeof(bool) != sizeof (int)), ...

I do not immediately see what integer-promotion problems you refer
too, neither do I understand what the size has to do with it (unless
you read old binary files).  Please clarify, so we can find any
potential problems.

     -- Dag Bruck




Author: dag@control.lth.se (Dag Bruck)
Date: 18 Nov 1994 19:58:14 GMT
Raw View
>>>>> "S" == Stephen Parker <sp106@ebor.york.ac.uk> writes:

S> Yes, but is this bool type a built in in the way that say int is,
S> or is it a user defined type, ie a class.

Bool is a new built-in integral type which takes the values "false"
and "true".  False is promoted to zero and true to one.  A zero value
can be (implicitly) converted to false, any non-zero value to true.

     -- Dag Bruck




Author: swf@elsegundoca.ncr.com (Stan Friesen)
Date: Fri, 18 Nov 1994 22:50:35 GMT
Raw View
In article <1994Nov18.114246.6532@leeds.ac.uk>, sp106@ebor.york.ac.uk (Stephen Parker) writes:
|>
|> Yes, but is this bool type a built in in the way that say int is, or is it a
|> user defined type, ie a class.

It is truly built-in, that is how if() can be made to expect it.

|> My point is that the type bool is so
|> fundamental to the concept of conditionals that it is what should be tested.
|> The statement if(x) {...} should be read as if(bool(x)){...} at the lowest
|> level, not the current standard (ARM90,$6.4.1) which states that the expression
|> within an if must be "of arithmetic of pointer type, or a class type for which
|> an unambiguous conversion to arithmetic or pointer type exists."

This has been changed.  'if()' now means pretty much exacly what you
suggest above.  (I doubt the draft words it that way, but the meaining
is essentially the same).

ARM is *way* out of date at this point, many things have been added
or changed (the library, bool, wchar_t, namespaces, RTTI, template
instantiation rules, and much more).

|> Is this what is proposed, ...

Yep, you described the proposal pretty much exactly as approved.

--
swf@elsegundoca.ncr.com  sarima@netcom.com

The peace of God be with you.




Author: kevlin@wslint.demon.co.uk (Kevlin Henney)
Date: Mon, 21 Nov 1994 09:19:45 +0000
Raw View
In article <1994Nov18.114246.6532@leeds.ac.uk>
           sp106@ebor.york.ac.uk "Stephen Parker" writes:

>Hans Walheim (hans@iar.se) wrote:
[...]
>Yes, but is this bool type a built in in the way that say int is, or is it a
>user defined type, ie a class.  My point is that the type bool is so
>fundamental to the concept of conditionals that it is what should be tested.
>The statement if(x) {...} should be read as if(bool(x)){...} at the lowest
>level, not the current standard (ARM90,$6.4.1) which states that the expression
>within an if must be "of arithmetic of pointer type, or a class type for which
>an unambiguous conversion to arithmetic or pointer type exists."  My suggestion
>is that the expression must be of boolean type (I shall let you choose its
> name)
>or of any class with unambiguous conversion to boolean; this includes the
>conversion of integer or pointer classes whilst allowing user defined classes
>to return a more natural type for validity checking.  This seems also a more
>fundamental proposal than that since the checking for True/False value of an
>integer is artificial.  The point of C++/Data abstraction/OOP is to allow
>operations to occur independent of physical representation of data; to then
>suggest that a conditional must be forced to intger or pointer type before
>conditions seems aginst this notion.
>
>Is this what is proposed, or is a standard bool class proposed in the style of
>the standard string class say.  I already have a boolean class that performs
>as well as the current language specification allows, a standardised version
>of this is a help but does not answer the problem I have describled.

It is not possible to write a bool class that behaves like a proper Boolean -
many have tried, all have failed. This is one reason (among many) that bool
has been accepted as a fundamental type, ie. it satisfies all of your queries.

+---------------------------+-------------------------------------------+
| Kevlin A P Henney         | Human vs Machine Intelligence:            |
| kevlin@wslint.demon.co.uk | Humans can wreck a nice beach more easily |
| Westinghouse Systems Ltd  |                                           |
+---------------------------+-------------------------------------------+




Author: hans@iar.se (Hans Walheim)
Date: Mon, 21 Nov 1994 13:03:20 GMT
Raw View
In article <1994Nov18.114246.6532@leeds.ac.uk> sp106@ebor.york.ac.uk (Stephen Parker) writes:
>From: sp106@ebor.york.ac.uk (Stephen Parker)
>Subject: Re: Boolean type: Limited usefullness
>Date: Fri, 18 Nov 1994 11:42:46 +0000 (GMT)

>Hans Walheim (hans@iar.se) wrote:
>: In article <JASON.94Nov17104622@phydeaux.cygnus.com> jason@cygnus.com (Jason Merrill) writes:
>: >>>>>> Stephen Parker <sp106@ebor.york.ac.uk> writes:

>: >> Is there any suggestion (or has there indeed already been), to make the
>: >> conditional explicitly depend on Booleans, and not integers.

>: >Yes.  The type you are looking for is 'bool', which is implemented in g++;
>: >I don't know whether or not it is implemented in other compilers.


>: The type 'bool' is now in the ANSI-C++ draft so all 'modern' C++ compilers
>: has it or will have it.

>Yes, but is this bool type a built in in the way that say int is, or is it a
>user defined type, ie a class.

It is a built-in type.


>My point is that the type bool is so
>fundamental to the concept of conditionals that it is what should be tested.
>The statement if(x) {...} should be read as if(bool(x)){...} at the lowest
>level, not the current standard (ARM90,$6.4.1) ...


As I said earlier, there is still 'dark corners' but I think the intent
is to implement what you are describing.  Currently this is not the case:

o  the relational operators are returning values of type 'bool',

o  the 'condition' clause in 'if (condition)' has the value of the
expression,


o  the 'condition' in 'for ( ; condition ; )' is converted to 'bool'


>operations to occur independent of physical representation of data; to then
>suggest that a conditional must be forced to intger or pointer type before
>conditions seems aginst this notion.


If the expression has type 'bool' then it will not be converted.

>Is this what is proposed, or is a standard bool class proposed in the style of
>the standard string class say.  I already have a boolean class that performs
>as well as the current language specification allows, a standardised version
>of this is a help but does not answer the problem I have describled.

>Stephen.

'bool' should/will be the answer to you problem !

/Hans




Author: sp106@ebor.york.ac.uk (Stephen Parker)
Date: Mon, 21 Nov 1994 13:10:49 +0000 (GMT)
Raw View
Kevlin Henney (kevlin@wslint.demon.co.uk) wrote:
: In article <1994Nov18.114246.6532@leeds.ac.uk>
:            sp106@ebor.york.ac.uk "Stephen Parker" writes:

: >Hans Walheim (hans@iar.se) wrote:
: >I already have a boolean class that performs
: >as well as the current language specification allows, a standardised version
: >of this is a help but does not answer the problem I have describled.

: It is not possible to write a bool class that behaves like a proper Boolean -
: many have tried, all have failed. This is one reason (among many) that bool
: has been accepted as a fundamental type, ie. it satisfies all of your queries.

In what way is it not possible to write a proper Boolean? I would like to
think that my 'Boolean' type performs acceptably -- not withstanding the
problem that has been discussed of most c++ imnplementations of the if(...)
condition currently not accepting a boolean type ( I think this change is one
of the most welcome ).  What other problems are there?


Stephen.

ps. Thank you to those who have helped clear up this problem for me.


--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~  i really don't know what i'm doing here         ~~~~~~~~~~~~~~~~~~~~~~
~  i really think i should've gone to bed tonight  ~  SP106@UK.AC.YORK  ~
~  but... just one drink                           ~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




Author: hans@iar.se (Hans Walheim)
Date: Mon, 21 Nov 1994 13:28:06 GMT
Raw View
In article <hans.27.2ED09A98@iar.se> hans@iar.se (Hans Walheim) writes:
>From: hans@iar.se (Hans Walheim)
>Subject: Re: Boolean type: Limited usefullness
>Date: Mon, 21 Nov 1994 13:03:20 GMT


>o  the 'condition' clause in 'if (condition)' has the value of the
>expression,


I am sorry about this - ALL 'condition' in 'testing statements'
('if', 'while', 'do' etc etc) has the 'condition' converted to bool.

(I missed reading a section in the ANSI-draft when trying to be fast and
efficient :)

/Hans




Author: sp106@ebor.york.ac.uk (Stephen Parker)
Date: Thu, 17 Nov 1994 10:40:43 +0000 (GMT)
Raw View
I have for some while been using a 'Boolean' type for boolean variables
similar to that suggested in 'The Design and Evolution of C++', but have
come to the concludision that they are of limited use: The problem being
that although I return True or False objects of the boolean type, this is
not what is switched upon in conditionals but the integer cast operator of
my boolean is used.

Now consider a class, X, which we wish to test, in a similar way to the io
classes, as we would use:

 fstream fs("foo") ;
 if(foo) { ... }

To allow this for our class X we require a Boolean cast, X::operator const
Boolean() const ;  But in use this requires casting X twice, which we are
unable to do implicitly.

Is there any suggestion (or has there indeed already been), to make the
conditional explicitly depend on Booleans, and not integers.  The free
conversion of integers and pointers to Booleans would keep the one implicit
cast only rule, whilst allowing classes to return a type more in line with
what is beeing test: is something valid or not.  Rather than the false use
of an integer.

Stephen.


--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~  i really don't know what i'm doing here         ~~~~~~~~~~~~~~~~~~~~~~
~  i really think i should've gone to bed tonight  ~  SP106@UK.AC.YORK  ~
~  but... just one drink                           ~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




Author: jason@cygnus.com (Jason Merrill)
Date: Thu, 17 Nov 1994 18:46:22 GMT
Raw View
>>>>> Stephen Parker <sp106@ebor.york.ac.uk> writes:

> Is there any suggestion (or has there indeed already been), to make the
> conditional explicitly depend on Booleans, and not integers.

Yes.  The type you are looking for is 'bool', which is implemented in g++;
I don't know whether or not it is implemented in other compilers.

Jason




Author: dag@control.lth.se (Dag Bruck)
Date: 18 Nov 1994 07:38:30 GMT
Raw View
>>>>> "S" == Stephen Parker <sp106@ebor.york.ac.uk> writes:

S> Is there any suggestion (or has there indeed already been), to make
S> the conditional explicitly depend on Booleans, and not integers.

It has already been done.  The C++ standard working paper says that
conditionals take a "bool" value.  Few compilers have implemented it
yet.

     -- Dag Bruck




Author: hans@iar.se (Hans Walheim)
Date: Fri, 18 Nov 1994 08:04:43 GMT
Raw View
In article <JASON.94Nov17104622@phydeaux.cygnus.com> jason@cygnus.com (Jason Merrill) writes:

>>>>>> Stephen Parker <sp106@ebor.york.ac.uk> writes:

>> Is there any suggestion (or has there indeed already been), to make the
>> conditional explicitly depend on Booleans, and not integers.

>Yes.  The type you are looking for is 'bool', which is implemented in g++;
>I don't know whether or not it is implemented in other compilers.


The type 'bool' is now in the ANSI-C++ draft so all 'modern' C++ compilers
has it or will have it.  There is still some 'dark corners' in how to
implement different operations on type 'bool', e.g. '++' is allowed and
will set any object of type 'bool' to 'true' but the decrement-
operation '--' is not allowed for type 'bool'.

You may also run into problems with integer-promotion (if sizeof(bool) !=
sizeof (int)), but all this is 'intermediate instabilities' that will be
fixed in '95 when the ANSI-C++ standard is ready :).

/Hans




Author: sp106@ebor.york.ac.uk (Stephen Parker)
Date: Fri, 18 Nov 1994 11:42:46 +0000 (GMT)
Raw View
Hans Walheim (hans@iar.se) wrote:
: In article <JASON.94Nov17104622@phydeaux.cygnus.com> jason@cygnus.com (Jason Merrill) writes:
: >>>>>> Stephen Parker <sp106@ebor.york.ac.uk> writes:

: >> Is there any suggestion (or has there indeed already been), to make the
: >> conditional explicitly depend on Booleans, and not integers.

: >Yes.  The type you are looking for is 'bool', which is implemented in g++;
: >I don't know whether or not it is implemented in other compilers.


: The type 'bool' is now in the ANSI-C++ draft so all 'modern' C++ compilers
: has it or will have it.  There is still some 'dark corners' in how to
: implement different operations on type 'bool', e.g. '++' is allowed and
: will set any object of type 'bool' to 'true' but the decrement-
: operation '--' is not allowed for type 'bool'.

: You may also run into problems with integer-promotion (if sizeof(bool) !=
: sizeof (int)), but all this is 'intermediate instabilities' that will be
: fixed in '95 when the ANSI-C++ standard is ready :).

: /Hans

Yes, but is this bool type a built in in the way that say int is, or is it a
user defined type, ie a class.  My point is that the type bool is so
fundamental to the concept of conditionals that it is what should be tested.
The statement if(x) {...} should be read as if(bool(x)){...} at the lowest
level, not the current standard (ARM90,$6.4.1) which states that the expression
within an if must be "of arithmetic of pointer type, or a class type for which
an unambiguous conversion to arithmetic or pointer type exists."  My suggestion
is that the expression must be of boolean type (I shall let you choose its name)
or of any class with unambiguous conversion to boolean; this includes the
conversion of integer or pointer classes whilst allowing user defined classes
to return a more natural type for validity checking.  This seems also a more
fundamental proposal than that since the checking for True/False value of an
integer is artificial.  The point of C++/Data abstraction/OOP is to allow
operations to occur independent of physical representation of data; to then
suggest that a conditional must be forced to intger or pointer type before
conditions seems aginst this notion.


Is this what is proposed, or is a standard bool class proposed in the style of
the standard string class say.  I already have a boolean class that performs
as well as the current language specification allows, a standardised version
of this is a help but does not answer the problem I have describled.

Stephen.
--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~  i really don't know what i'm doing here         ~~~~~~~~~~~~~~~~~~~~~~
~  i really think i should've gone to bed tonight  ~  SP106@UK.AC.YORK  ~
~  but... just one drink                           ~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~