Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-02-2008, 01:21 AM
oops
 
Posts: n/a
why would i use '!!' ?

greetings,

I have seen this in inherited code (paraphrased for _some_
readability):

int bar( void );

bool foo( void )
{
bool brv = bar();

return( !! brv);
}

What reason, if any, would one use the double bang: ! ! ?
Justification (other than obfuscation) escape me...

Thanks!


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #2 (permalink)  
Old 08-02-2008, 09:33 AM
Daniel T.
 
Posts: n/a
Re: why would i use '!!' ?

oops <brokenCollie@gmail.com> wrote:

> greetings,
>
> I have seen this in inherited code (paraphrased for _some_
> readability):
>
> int bar( void );
>
> bool foo( void )
> {
> bool brv = bar();
>
> return( !! brv);
> }
>
> What reason, if any, would one use the double bang: ! ! ?
> Justification (other than obfuscation) escape me...


In this case, the double bangs just obfuscate. I could see someone using
that construct to convert a non-zero int into a '1', but even that would
be a rather strange thing to do IMHO.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #3 (permalink)  
Old 08-02-2008, 09:35 AM
JoshuaMaurice@gmail.com
 
Posts: n/a
Re: why would i use '!!' ?

On Aug 1, 5:21 pm, oops <brokenCol...@gmail.com> wrote:
> greetings,
>
> I have seen this in inherited code (paraphrased for _some_
> readability):
>
> int bar( void );
>
> bool foo( void )
> {
> bool brv = bar();
>
> return( !! brv);
>
> }
>
> What reason, if any, would one use the double bang: ! ! ?
> Justification (other than obfuscation) escape me...


{ Edits: quoted signature & clc++m banner removed, please don't quote extraneous
material. - mod }

The first operator ! (first by associativity) might be overloaded, a
user defined function on a class, and they happen to want to return
the opposite truth-value of that.
This
return ( !! brv);
is
return ( ! ( ! brv));
could be
return ( ! ( brv . operator ! ()));

or it could be
return ( ! ( operator !(brv)); //where operator ! is a user
defined operator

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #4 (permalink)  
Old 08-02-2008, 09:35 AM
Patrick May
 
Posts: n/a
Re: why would i use '!!' ?

oops <brokenCollie@gmail.com> writes:
> greetings,
>
> I have seen this in inherited code (paraphrased for _some_
> readability):
>
> int bar( void );
>
> bool foo( void )
> {
> bool brv = bar();
>
> return( !! brv);
> }
>
> What reason, if any, would one use the double bang: ! ! ?
> Justification (other than obfuscation) escape me...


The last time I saw something like that it was intended to ensure
that the return value was either 0 or 1, but since the type of brv is
already bool that shouldn't be an issue.

Regards,

Patrick

------------------------------------------------------------------------
S P Engineering, Inc. | Large scale, mission-critical, distributed OO
| systems design and implementation.
pjm@spe.com | (C++, Java, Common Lisp, Jini, middleware, SOA)

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #5 (permalink)  
Old 08-02-2008, 09:36 AM
Hakusa@gmail.com
 
Posts: n/a
Re: why would i use '!!' ?

On Aug 1, 7:21 pm, oops <brokenCol...@gmail.com> wrote:
> What reason, if any, would one use the double bang: ! ! ?


It's one of the many solutions to the "how do I check if my data is
valid by using the simple syntax if( x )?", but I can't remember why
it used double !'s instead of one... Maybe because the bang is
idiomatically supposed to return false if true, so double bangs
returns true is true?

It's also a shorter way of writing:
x = x? 1 : 0;
x = !!x;
if for any reason, x must only equal one if true.

> Justification (other than obfuscation) escape me...


Yeah, I wouldn't use it under normal circumstances. Maybe if someone
pointed a gun to my head, but even then, it'd have to be a pointy gun.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #6 (permalink)  
Old 08-02-2008, 09:38 AM
John Whorfin
 
Posts: n/a
Re: why would i use '!!' ?

oops wrote:
> int bar( void );

....
> bool brv = bar();
> return( !! brv);

....

> What reason, if any, would one use the double bang: ! ! ?


The expression "!! <int>" will force the value of <int> to be either
zero or one, i.e. non-zero <int> is "clamped" to one. The conversion
to bool in the intialization of "brv" and return from a bool function
should make it moot.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #7 (permalink)  
Old 08-02-2008, 11:28 AM
Thomas Richter
 
Posts: n/a
Re: why would i use '!!' ?

oops wrote:
> greetings,
>
> I have seen this in inherited code (paraphrased for _some_
> readability):
>
> int bar( void );
>
> bool foo( void )
> {
> bool brv = bar();
>
> return( !! brv);
> }
>
> What reason, if any, would one use the double bang: ! ! ?
> Justification (other than obfuscation) escape me...


Are you sure you quoted the code correctly? If brv would be an int (not
a bool) then this is a conversion from int (or other types) to bool.

So long,
Thomas

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #8 (permalink)  
Old 08-02-2008, 11:28 AM
Chris Morley
 
Posts: n/a
Re: why would i use '!!' ?

> greetings,
>
> I have seen this in inherited code (paraphrased for _some_
> readability):
>
> int bar( void );
> bool foo( void )
> {
> bool brv = bar();
> return( !! brv);
> }
>
> What reason, if any, would one use the double bang: ! ! ?
> Justification (other than obfuscation) escape me...


It forces a conversion to bool as the result of "!expr" is a bool, soo
"!!expr is" bool too.

"return (!!bar());" would be a more useful example. In your example, most
compilers will warn you on your conversion from int to bool. Adding a
"(bool)bar()" is clearly too much typing for a C/C++ programmer so !! is 4
chars shorter.

Chris



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #9 (permalink)  
Old 08-02-2008, 11:28 AM
Jack Klein
 
Posts: n/a
Re: why would i use '!!' ?

On Fri, 1 Aug 2008 18:21:43 CST, oops <brokenCollie@gmail.com> wrote
in comp.lang.c++.moderated:

> greetings,
>
> I have seen this in inherited code (paraphrased for _some_
> readability):
>
> int bar( void );
>
> bool foo( void )
> {
> bool brv = bar();
>
> return( !! brv);
> }
>
> What reason, if any, would one use the double bang: ! ! ?
> Justification (other than obfuscation) escape me...


There is actually no reason at all to use it in standard C++, or any
version of C that conforms to the 1999 or later version of the
standard. These have a true Boolean type, which is always set to true
or false when assigned to.

But in olden days there many kludges like this:

typedef enum { FALSE, TRUE } BOOL;

Where BOOL wound up an integer type. That actually worked quite well
in practice, as long as everybody used it in a pure Boolean context:

if (bar())
/* do something */
else
/* do something else */

But there's always somebody who wants to write:

if (bar() == TRUE)

.....looking for a specific numeric value, instead of the generic 0 is
false and anything else is true.

So to make sure that a function returning the old-style BOOL, based on
calling a function like bar(), that could return 0 or a number of
non-zero values, you needed to something like this:

brv = (bar() != 0)

.....or:

brv = bar();
!!brv;

The latter is equivalent to:

!(!brv);

So, if brv = 0, the first ! makes it 1, and the second ! makes it 0.
And if brv is anything other than 0, the first ! makes it 0 and the
second ! makes it 1 (TRUE).

In other words, you can consider !! as the "Booleanization" operator,
producing (in C) the integer value 0 or 1, and in C++ the Boolean
value true or false.

It's possible that your legacy code originally used some typedef or
macro like BOOL, and was later updated to use the standard bool type.
Or it was written with the bool type by somebody who had written or
seen a lot of the older kludges from before there was a real bool
type.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #10 (permalink)  
Old 08-02-2008, 05:41 PM
Alf P. Steinbach
 
Posts: n/a
Re: why would i use '!!' ?

* oops:
> greetings,
>
> I have seen this in inherited code (paraphrased for _some_
> readability):
>
> int bar( void );
>
> bool foo( void )
> {
> bool brv = bar();
>
> return( !! brv);
> }
>
> What reason, if any, would one use the double bang: ! ! ?
> Justification (other than obfuscation) escape me...


I know of two reasons.

One is to explicitly convey the indenteded effect, a conversion ->bool. This is
in the same way as indenting the code is usually regarded as helpful. It doesn't
help the compiler, but the reader. It will always be more clear, not less.
Logic: it cannot obfuscate for a non-novice since it's a basic operation and
exactly the operation that will be performed anyway, and a novice who does not
understand what the bang-bang operation is really needs it.

The other reason is more arguable, to produce code that compiles cleanly with no
warnings, so that any warning can be taken seriously, regarded as a "real"
warning. Visual C++ is one popular compiler that, unfortunately, warns about
implicit conversions to bool. Avoiding silly-warnings is arguable because it is
not /always/ practically possible, while in a large number of cases it is.


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 03:55 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Debt Help | Mortgages | Web Advertising | Charity | Mortgages



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114