Hey, that makes a whole lot of sense! Thanks Pat! =)

-Nick


-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:hlcoders-admin@;list.valvesoftware.com]On Behalf Of Pat Magnan
Sent: Friday, October 18, 2002 2:23 PM
To: [EMAIL PROTECTED]
Subject: RE: [hlcoders] little bug in the SDK 2.3 ?


!! seems sort of obtuse, however, there are situations in which you'd want
to use it.

It says ! ( ! (something) ).

Truth table:

something     ! something     !!something
true                   0                     1
false                  1                     0

Why would we want to do this? If you've got an int, that's either 0 or
non-zero, and you want to return it to a bool type, you can get warnings
back from the compiler if you just pass one to the other.

A lot of beginning programmers mistakenly believe that 'true' expressions
== 1, this is not the case, 'truth' in C is simply non-zero. bool C++ data
types must always act as if they are 0 or 1, so !!something where something
is an int, behaves correctly as a bool (it's either 0 or 1 as demonstrated
above).

Lookup the C4800 compiler warning for more information on why passing ints
to bools can be bad.

Returning the value of a comparison operation is hard on the brain first
time you see it, it's an old trick, and part of the language. You can
return the results of a computation right? This is the same thing, we're
just not adding, we're using logical operators on it.

If you don't like it, or the code isn't self evident to you, declare this
on a few more lines, and don't worry about it.

However, it's no different than this perfectly valid syntax:

int MyAdd(int a, int b)
{
    return a + b;
}

Or this:

bool MyTest( int c )
{
      return (!!c);   // 0 or 1 depending on c's 'trueness'
}

Why we don't go through extra assignments is just a matter of a small
optimization in memory we're utilizing. You could allocate temporary values
in your functions, and unless you're calling them 5 bizzillion times in a
loop, write your functions that way if it is more clear to you.

Just because it CAN be written more optimally, doesn't mean it has to be,
because programmer efficiency is important too (I mean, not writing code
that's so nifty that in 3 months you don't have the foggiest idea what you
were getting at).

Pat 'sluggo' Magnan
Tour of Duty mod
http://www.tourofdutymod.com

At 01:05 PM 10/17/2002 -0400, you wrote:

>Sorry for the newbie C++ coder question, but wouldn't it be wrong to use a
>comparison on a return statement like was suggested? Is the !! an operator?
>I haven't seen it before. From this thread I'm guessing that it converts
any
> >0 value to a 1 and any <0 value to 0, making the return perform an
>assignment before returning? Just a guess. I know this is a little OT I
>guess, so feel free to answer me off-list if you know. Thanks! =)
>
>-Nick
>
>
>-----Original Message-----
>From: [EMAIL PROTECTED]
>[mailto:hlcoders-admin@;list.valvesoftware.com]On Behalf Of Mark Gornall
>Sent: Thursday, October 17, 2002 12:43 PM
>To: '[EMAIL PROTECTED]'
>Subject: RE: [hlcoders] little bug in the SDK 2.3 ?
>
>
>valve use '!!' a few times in the sdk so it's probably deliberate.
>
>
>-----Original Message-----
>From: Cortex [mailto:cort@;meloo.com]
>Sent: 17 October 2002 16:51
>To: [EMAIL PROTECTED]
>Subject: Re: [hlcoders] little bug in the SDK 2.3 ?
>
>
>Oh yes, it could be a good explanation.
>But, why wouldn't it be :
>return (gEngfuncs.GetPlayerUniqueID( iPlayer, playerID ) != 0);
>
>? It's much more concise... And it doesn't look as a mistake :p
>
>      - Cortex : HL Albator coder and mapper ( www.hlalbator.fr.st )
>      - email : [EMAIL PROTECTED]  &  ICQ : 71548738
>
>David Flor <[EMAIL PROTECTED]> nous disait :
>
> > Don't know if it's meant to be that way, but the "!!" convention
> > ensures that the value is ALWAYS either a one or a zero.
> >
>
>__________________________________________________________
>This mail has been scanned for all known viruses by UUNET
>delivered through the MessageLabs Virus Control Centre.
>_______________________________________________
>To unsubscribe, edit your list preferences, or view the list archives,
>please visit:
>http://list.valvesoftware.com/mailman/listinfo/hlcoders
>
>
>_______________________________________________
>To unsubscribe, edit your list preferences, or view the list archives,
>please visit:
>http://list.valvesoftware.com/mailman/listinfo/hlcoders

_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives,
please visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders


_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives, please visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders

Reply via email to