Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: equality op...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 30 of 45 Topic 45751 of 46738
Post > Topic >>

Re: equality operator question

by James Kanze <james.kanze@[EMAIL PROTECTED] > May 6, 2008 at 03:04 AM

On May 5, 5:07 pm, p...@[EMAIL PROTECTED]
 (Pascal J. Bourguignon)
wrote:
> Krice <pau...@[EMAIL PROTECTED]
> writes:
> > On 5 touko, 16:19, James Kanze <james.ka...@[EMAIL PROTECTED]
> wrote:
> >> First of all, you never compare with true or false.

> > I'm doing that. What could go wrong?

> Well, with false, not much wrong, since false must be =3D=3D !true, and
> if(false){this;}else{that;} must evaluate that, we're about certain
> that false =3D=3D 0.

False is equal to false.  It's never 0, or any other numeric
value.  If you convert a bool to another numeric type, false
converts to 0, but that's a different problem.  (Also, you
rarely would convert a bool to any other numeric type.))

> But for true, it's something else.  It may be 1, -1, 0xff, or
> whatever not 0.

True is true.  It's never 1, -1, 0xff or whatevert.  It's not a
numeric value.

False and true are values of type bool.  Type bool has exactly
two values, false, and true, and no others.  A expression of
type bool cannot have a value 0, or 1, or -1 or 0xFF or
whatever.  It can only have a value of false or true.

In C++, for historical reasons, bool converts to numeric types,
with false converting to 0, and true to 1.  In C++, for
historical reasons, numeric types and pointers convert to bool,
with the result being the same as if you'd compared them to 0.
These conversions are really only present for historical
reasons, however, and you don't use them in well written code.

> But in any case, it's harder to read boolean expression when
> you add equivalences with the constants.

The real problem is that it is redundant.  Suppose you write
something like:

    if ( a =3D=3D true )

Presumably, a has type bool, or the expression doesn't really
make sense.  (If a has type int, then the expression "true", of
type bool, will be converted to the integral value 1.  If this
is what is wanted, it's obfuscation.)  But of course, the
expression "a =3D=3D true" also has type bool.  So by the same
logic, if we wanted to test whether it was true, we'd write:

    if ( (a =3D=3D true) =3D=3D true )

Except, of course, that "(a =3D=3D true) =3D=3D true" has type bool, so
we'll write...

A condition requires a bool (or something which converts
implicitly to bool).  If you've got a bool, you've already got
what you need.  If you don't have a bool, the comparison
operators (=3D=3D, !=3D, > etc.) all result in a bool.

--
James Kanze (GABI Software)             email:james.kanze@[EMAIL PROTECTED]
 en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
 




 45 Posts in Topic:
equality operator question
"barcaroller" &  2008-05-04 22:40:28 
Re: equality operator question
Jack Klein <jackklein@  2008-05-04 21:51:54 
Re: equality operator question
"Jim Langston"   2008-05-05 02:51:12 
Re: equality operator question
"kwikius" <a  2008-05-05 11:58:28 
Re: equality operator question
James Kanze <james.kan  2008-05-05 06:19:44 
Re: equality operator question
pjb@[EMAIL PROTECTED] (P  2008-05-05 15:28:16 
Re: equality operator question
James Kanze <james.kan  2008-05-05 07:12:07 
Re: equality operator question
Krice <paulkp@[EMAIL P  2008-05-05 07:48:06 
Re: equality operator question
pjb@[EMAIL PROTECTED] (P  2008-05-05 17:07:23 
Re: equality operator question
Eberhard Schefold <eba  2008-05-06 08:02:24 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-06 06:53:14 
Re: equality operator question
=?UTF-8?B?RXJpayBXaWtzdHL  2008-05-06 16:08:43 
Re: equality operator question
"kwikius" <a  2008-05-06 17:32:37 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-06 13:39:06 
Re: equality operator question
Eberhard Schefold <ebe  2008-05-06 19:46:31 
Re: equality operator question
"Bo Persson" &l  2008-05-06 20:00:21 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-06 15:18:44 
Re: equality operator question
=?UTF-8?B?RXJpayBXaWtzdHL  2008-05-06 18:17:40 
Re: equality operator question
ram@[EMAIL PROTECTED] (S  2008-05-07 22:02:04 
Re: equality operator question
Jerry Coffin <jcoffin@  2008-05-07 22:38:59 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-05 12:32:00 
Re: equality operator question
peter koch <peter.koch  2008-05-05 11:08:13 
Re: equality operator question
Krice <paulkp@[EMAIL P  2008-05-05 15:33:29 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-05 20:36:41 
Re: equality operator question
"barcaroller" &  2008-05-05 23:01:32 
Re: equality operator question
Christian Hackl <hacki  2008-05-06 09:34:51 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-06 06:49:33 
Re: equality operator question
Krice <paulkp@[EMAIL P  2008-05-05 23:58:01 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-06 06:52:13 
Re: equality operator question
James Kanze <james.kan  2008-05-06 03:04:22 
Re: equality operator question
Eberhard Schefold <ebe  2008-05-06 16:04:19 
Re: equality operator question
pjb@[EMAIL PROTECTED] (P  2008-05-06 16:23:15 
Re: equality operator question
Eberhard Schefold <ebe  2008-05-06 16:35:26 
Re: equality operator question
ram@[EMAIL PROTECTED] (S  2008-05-06 14:48:53 
Re: equality operator question
Eberhard Schefold <ebe  2008-05-06 17:13:01 
Re: equality operator question
James Kanze <james.kan  2008-05-06 03:09:18 
Re: equality operator question
James Kanze <james.kan  2008-05-06 05:06:53 
Re: equality operator question
Nick Keighley <nick_ke  2008-05-06 09:06:24 
Re: equality operator question
Krice <paulkp@[EMAIL P  2008-05-06 11:50:27 
Re: equality operator question
James Kanze <james.kan  2008-05-07 01:53:25 
Re: equality operator question
ram@[EMAIL PROTECTED] (S  2008-05-07 15:16:59 
Re: equality operator question
James Kanze <james.kan  2008-05-09 04:34:21 
Re: equality operator question
Lionel B <me@[EMAIL PR  2008-05-06 14:31:59 
Re: equality operator question
Eberhard Schefold <ebe  2008-05-06 16:36:25 
Re: equality operator question
Lionel B <me@[EMAIL PR  2008-05-07 09:08:00 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Jul 9 0:10:02 CDT 2008.