Hi,

Can the == operator be used with bool? Is this valid?

////////////
bool a, b;

bool c = a == b;
////////////

Thanks,
Nelson

Re: bool and == by Alexander

Alexander
Fri Jun 18 21:05:22 CDT 2004

Absolutely.

Result of == operator is of type 'bool' in C++.

"Nelson" <doomer999@hotmail.com> wrote in message
news:%23wya3BaVEHA.2592@TK2MSFTNGP09.phx.gbl...
> Hi,
>
> Can the == operator be used with bool? Is this valid?
>
> ////////////
> bool a, b;
>
> bool c = a == b;
> ////////////
>
> Thanks,
> Nelson
>
>



Re: bool and == by Nelson

Nelson
Sat Jun 19 02:39:54 CDT 2004

Thank you for your answer.

I've been using the == operator for other types, but it just feels strange
to use it with bool type. I mean the expression

true == true

seems weird to me.

But now, thanks to your reply, I know it's no problem.
I appreciate your help.

Nelson


"Alexander Grigoriev" <alegr@earthlink.net> ¦b¶l¥ó
news:uo$niHaVEHA.4048@TK2MSFTNGP12.phx.gbl ¤¤¼¶¼g...
> Absolutely.
>
> Result of == operator is of type 'bool' in C++.
>
> "Nelson" <doomer999@hotmail.com> wrote in message
> news:%23wya3BaVEHA.2592@TK2MSFTNGP09.phx.gbl...
> > Hi,
> >
> > Can the == operator be used with bool? Is this valid?
> >
> > ////////////
> > bool a, b;
> >
> > bool c = a == b;
> > ////////////
> >
> > Thanks,
> > Nelson
> >
> >
>
>



Re: bool and == by Arnaud

Arnaud
Sat Jun 19 04:17:55 CDT 2004

Nelson wrote:
> Thank you for your answer.
>
> I've been using the == operator for other types, but it just feels
> strange to use it with bool type. I mean the expression
>
> true == true
>
> seems weird to me.

Yup, as weird as (int)1 == (int) 1; ! ;-)

Arnaud



Re: bool and == by Chen

Chen
Sat Jun 19 09:26:25 CDT 2004

But... what happens if you do this...


int iA = 2;
bool bA;
bA = iA;


is bA == true there? That doesn't work with BOOL (which is typdefed to
"int")... :-)
And, what happens if you do this?


memset(&bA,2,sizeof(bool));



// Chen

"Arnaud Debaene" <adebaene@club-internet.fr> skrev i meddelandet
news:OLEVF5dVEHA.1152@TK2MSFTNGP09.phx.gbl...
> Nelson wrote:
> > Thank you for your answer.
> >
> > I've been using the == operator for other types, but it just feels
> > strange to use it with bool type. I mean the expression
> >
> > true == true
> >
> > seems weird to me.
>
> Yup, as weird as (int)1 == (int) 1; ! ;-)
>
> Arnaud
>
>



Re: bool and == by Arnaud

Arnaud
Sat Jun 19 09:38:59 CDT 2004

Chen wrote:
> But... what happens if you do this...
>
>
> int iA = 2;
> bool bA;
> bA = iA;
>
>
> is bA == true there?
Yes. There is an implicit conversion from int to bool, mainly here for
historical reasons. 0 becomes false, and any non-zero value becomes true.

> And, what happens if you do this?
>
> memset(&bA,2,sizeof(bool));

Undefined. Just as if you would do :
float f;
memset(&f, 1, sizeof(float));

Why do you insist on having an integer representation for bool? bools are
either true or false, period.

Also, as Alexander has said, every == operation in C++ yeilds to abool,
whatever the compared types.

Arnaud
MVP - VC



Re: bool and == by Chen

Chen
Sun Jun 20 10:15:58 CDT 2004

No, no, you mistunderstood me. I don't like the fake integer bool at all...
It's a big waste of memory using them ;-). But, many library functions is
still using them...

// Chen

"Arnaud Debaene" <adebaene@club-internet.fr> skrev i meddelandet
news:OrdffsgVEHA.2544@TK2MSFTNGP10.phx.gbl...
> Chen wrote:
> > But... what happens if you do this...
> >
> >
> > int iA = 2;
> > bool bA;
> > bA = iA;
> >
> >
> > is bA == true there?
> Yes. There is an implicit conversion from int to bool, mainly here for
> historical reasons. 0 becomes false, and any non-zero value becomes true.
>
> > And, what happens if you do this?
> >
> > memset(&bA,2,sizeof(bool));
>
> Undefined. Just as if you would do :
> float f;
> memset(&f, 1, sizeof(float));
>
> Why do you insist on having an integer representation for bool? bools are
> either true or false, period.
>
> Also, as Alexander has said, every == operation in C++ yeilds to abool,
> whatever the compared types.
>
> Arnaud
> MVP - VC
>
>