Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Re: What does !! do? (http://www.velocityreviews.com/forums/t958016-re-what-does-do.html)

James Kanze 02-24-2013 05:38 PM

Re: What does !! do?
 
On Thursday, January 24, 2013 9:11:06 AM UTC, David Brown wrote:
> On 23/01/13 16:24, Bint wrote:


> > I haven't seen this syntax before. What does "!!" do in C++?


> > As in this statement


> > int t = !!(x & size);


> Think of it as a boolean "normalize" operator that will turn any
> non-zero "true" value into 1, and leave zero "false" as 0.


There is no such thing as a boolean "normalize" operator. An
expression which has type bool can only have one of two values:
false and true (*not* 0 and 1). The ! operator takes a bool,
and returns a bool. In the above expression, there is an
implicit conversion of "x & size" to bool; such implicit
conversions are simply obfuscation, and should be made explicit:

bool t = (x & size) != 0;

or if you really need an int, with values 0 and 1:

int t = (x & size) != 0 ? 1 : 0;

or

int t = static_cast<int>( (x & size) != 0 );

(But I've never actually encountered such a case.)

Balog Pal 02-24-2013 07:41 PM

Re: What does !! do?
 
On 2/24/2013 6:38 PM, James Kanze wrote:

>> Think of it as a boolean "normalize" operator that will turn any
>> non-zero "true" value into 1, and leave zero "false" as 0.


> There is no such thing as a boolean "normalize" operator. An
> expression which has type bool can only have one of two values:
> false and true (*not* 0 and 1).


And if you work with types that are not 'bool' but still boolean and in
the quote, that is why the normalisation is needed.

Guess you agree that we have abundant amount of code out there that
creates typedef to some boolean(-intended) type that maps to some other
integral type. Say for compatibility with old C.

And those can store other values, that are waiting to cause headache.



James Kanze 02-25-2013 12:24 PM

Re: What does !! do?
 
On Sunday, 24 February 2013 19:41:39 UTC, Balog Pal wrote:
> On 2/24/2013 6:38 PM, James Kanze wrote:


> >> Think of it as a boolean "normalize" operator that will turn any
> >> non-zero "true" value into 1, and leave zero "false" as 0.


> > There is no such thing as a boolean "normalize" operator. An
> > expression which has type bool can only have one of two values:
> > false and true (*not* 0 and 1).


> And if you work with types that are not 'bool' but still boolean and in
> the quote, that is why the normalisation is needed.


> Guess you agree that we have abundant amount of code out there that
> creates typedef to some boolean(-intended) type that maps to some other
> integral type. Say for compatibility with old C.


> And those can store other values, that are waiting to cause headache.


Even then, you'ld write something like "x != 0" (which is
guaranteed to return 0 or 1 in old C), rather than "!!x" (which
is guaranteed to confuse any reasonable person---"!!" is
a double negative: the second ! negates the first).

--
James

Rui Maciel 02-25-2013 12:43 PM

Re: What does !! do?
 
Balog Pal wrote:

> On 2/24/2013 6:38 PM, James Kanze wrote:
>
>>> Think of it as a boolean "normalize" operator that will turn any
>>> non-zero "true" value into 1, and leave zero "false" as 0.

>
>> There is no such thing as a boolean "normalize" operator. An
>> expression which has type bool can only have one of two values:
>> false and true (*not* 0 and 1).

>
> And if you work with types that are not 'bool' but still boolean and in
> the quote, that is why the normalisation is needed.


If those types are user-defined, isn't it possible to achieve the same
effect by providing conversion functions?


Rui Maciel

Balog Pal 02-25-2013 11:48 PM

Re: What does !! do?
 
On 2/25/2013 1:24 PM, James Kanze wrote:

>> And those can store other values, that are waiting to cause headache.

>
> Even then, you'ld write something like "x != 0"


I did that for some time then switched to the other in like mid 90's.

> (which is guaranteed to return 0 or 1 in old C)


Err, isn't !! guaranteed to return that too? I admittedly not very well
versed in pre-ANSI C, but am positive its result is mandatory 0 or 1
depending on how the operand originally compares to 0.

>, rather than "!!x" (which
> is guaranteed to confuse any reasonable person---"!!"


Sure term "reasonable person" is something subjective but I have about 2
decades of real experience that it did not confuse anyobne around me for
more than a few minutes. And after the first coin drop everyone used it
without problems on either reading or writing.

> is a double negative: the second ! negates the first).


In theory probably, but in practice it is read like a pattern -- like it
was a single operator. Same way you recognise a ton of
styles/idioms/whatever in code and learn to understand or avoid them.

As I said before I see no reason to ever see it in code that uses
"native" booleans like bool or _Bool, but pretty natural with BOOL of
WIN32 API (that is actually a 32 bit int, and gladly picks up any
pattern from type-punned things like WPARAM, LPARAM, LRESULT, or
handles/pointers floating around. (As a cherry on the cake there's also
MSVC's level 1 sillywarning about performance of converting int to
bool... ;)




Balog Pal 02-25-2013 11:54 PM

Re: What does !! do?
 
On 2/25/2013 1:43 PM, Rui Maciel wrote:
> Balog Pal wrote:
>
>> On 2/24/2013 6:38 PM, James Kanze wrote:
>>
>>>> Think of it as a boolean "normalize" operator that will turn any
>>>> non-zero "true" value into 1, and leave zero "false" as 0.

>>
>>> There is no such thing as a boolean "normalize" operator. An
>>> expression which has type bool can only have one of two values:
>>> false and true (*not* 0 and 1).

>>
>> And if you work with types that are not 'bool' but still boolean and in
>> the quote, that is why the normalisation is needed.

>
> If those types are user-defined, isn't it possible to achieve the same
> effect by providing conversion functions?


The "user" that defines them is the provider of the API or library (like
WIN32), and majority is typedef for some built-in integral type.





All times are GMT. The time now is 05:10 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.