![]() |
|
|
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. |
|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
![]() |
|
| Thread Tools | Search this Thread |
| Display Modes | |
|
|