Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Developing U++ » U++ Developers corner » What does !! in e.g. FtpClient class mean?
What does !! in e.g. FtpClient class mean? [message #39623] Wed, 10 April 2013 13:57 Go to next message
NilaT is currently offline  NilaT
Messages: 70
Registered: November 2011
Location: Austria
Member
Hello,

I just got a very noobish question.

What is the following Statement good for?
!!FtpDelete(path, ftpconn);

(This example is from the FtpClient class, but there maybe others)

!! = not not? Why use it then anyway?

Thanks for the advice.
Regards.

[Updated on: Wed, 10 April 2013 14:04]

Report message to a moderator

Re: What does !! in e.g. FtpClient class mean? [message #39624 is a reply to message #39623] Wed, 10 April 2013 14:39 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

NilaT wrote on Wed, 10 April 2013 13:57

I just got a very noobish question.

What is the following Statement good for?
!!FtpDelete(path, ftpconn);

(This example is from the FtpClient class, but there maybe others)

!! = not not? Why use it then anyway?

Hi NilaT,

This question appears here regulary, and it's not really noobish Smile What it in fact does is a cast to bool Smile The first negation causes imlicit cast to bool and the second one just returns the value to the original. Same as if you wrote
(bool)FtpDelete(path, ftpconn);

I'm not exactly sure why Mirek prefers writing it this way, but I strongly suspect the reason is that it is faster to write Smile You can find it on many places in U++ sources...

Best regards,
Honza
Re: What does !! in e.g. FtpClient class mean? [message #39625 is a reply to message #39623] Wed, 10 April 2013 14:50 Go to previous messageGo to next message
NilaT is currently offline  NilaT
Messages: 70
Registered: November 2011
Location: Austria
Member
Hi and thanks for the fast answer.

I tried to search the site, but "!!" or "not not" are not really good search terms Wink

So you cast int to bool with a "!"?
And then you return exctly the opposite?

So if the function returns 0 (which would be false in bool statement anyway, so why cast?) you convert it to bool, and then the second "!" returns true?
Is it right?

It's a bit confusing though Razz

[Updated on: Wed, 10 April 2013 14:52]

Report message to a moderator

Re: What does !! in e.g. FtpClient class mean? [message #39626 is a reply to message #39625] Wed, 10 April 2013 15:54 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Sometimes cast is necessary, e.g.:
int func(int n){
    return 1;
};
int func(bool b) {
    return 2;
}

int main() {
    int i=0;
    func(i) != func(!!i); // there is a difference ;-)
    func(!!i) == func((bool)i); // this is same ;-)
}


When you use '!' on anything else then bool, compiler will first cast it to bool (if possible) and then compute the negation. So !!x is equal to !( !( (bool)x ) ).

Honza
Re: What does !! in e.g. FtpClient class mean? [message #39627 is a reply to message #39623] Wed, 10 April 2013 16:27 Go to previous messageGo to next message
NilaT is currently offline  NilaT
Messages: 70
Registered: November 2011
Location: Austria
Member
Allllright, thanks for the answer Wink
Re: What does !! in e.g. FtpClient class mean? [message #39659 is a reply to message #39627] Mon, 15 April 2013 18:42 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
I am not FtpClient author and brief review of code indicates that in that particular case, using !! is redundant.

Anyway, sometimes it can have same benefit, see Prompt:

int Prompt(Callback1<const String&> WhenLink,
           const char *title, const Image& iconbmp, const char *qtf, bool okcancel,
           const char *button1, const char *button2, const char *button3,
		   int cx,
		   Image im1, Image im2, Image im3)
{
........
	int nbtn = !!button1 + !!button2 + !!button3;
Re: What does !! in e.g. FtpClient class mean? [message #39667 is a reply to message #39659] Tue, 16 April 2013 19:39 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
mirek wrote on Mon, 15 April 2013 12:42

I am not FtpClient author and brief review of code indicates that in that particular case, using !! is redundant.

Anyway, sometimes it can have same benefit, see Prompt:

int Prompt(Callback1<const String&> WhenLink,
           const char *title, const Image& iconbmp, const char *qtf, bool okcancel,
           const char *button1, const char *button2, const char *button3,
		   int cx,
		   Image im1, Image im2, Image im3)
{
........
	int nbtn = !!button1 + !!button2 + !!button3;



I can be wrong, but it is not guarantied that true == 1. You can only be sure that false == 0.


Regards,
Novo
Re: What does !! in e.g. FtpClient class mean? [message #39668 is a reply to message #39667] Tue, 16 April 2013 19:42 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Novo wrote on Tue, 16 April 2013 13:39

mirek wrote on Mon, 15 April 2013 12:42

I am not FtpClient author and brief review of code indicates that in that particular case, using !! is redundant.

Anyway, sometimes it can have same benefit, see Prompt:

int Prompt(Callback1<const String&> WhenLink,
           const char *title, const Image& iconbmp, const char *qtf, bool okcancel,
           const char *button1, const char *button2, const char *button3,
		   int cx,
		   Image im1, Image im2, Image im3)
{
........
	int nbtn = !!button1 + !!button2 + !!button3;



I can be wrong, but it is not guarantied that true == 1. You can only be sure that false == 0.


Actually, it IS guaranteed. Result of comparison operators and logical operators is always 0 or 1, since C was invented.

Mirek

[Updated on: Tue, 16 April 2013 19:42]

Report message to a moderator

Re: What does !! in e.g. FtpClient class mean? [message #39675 is a reply to message #39668] Tue, 16 April 2013 20:27 Go to previous message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
mirek wrote on Tue, 16 April 2013 13:42


Actually, it IS guaranteed. Result of comparison operators and logical operators is always 0 or 1, since C was invented.

Mirek


Thank you for the clarification. I found this in the C++ standard (4.7.4 Integral conversions). Now I have one more reason to file bug reports with compilers. Smile


Regards,
Novo
Previous Topic: IML including on multiple headers/cpp files
Next Topic: What does SSE2 usage enhance?
Goto Forum:
  


Current Time: Thu Mar 28 23:10:57 CET 2024

Total time taken to generate the page: 0.01037 seconds