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 |
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 |
|
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 What it in fact does is a cast to bool 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 You can find it on many places in U++ sources...
Best regards,
Honza
|
|
|
|
Re: What does !! in e.g. FtpClient class mean? [message #39626 is a reply to message #39625] |
Wed, 10 April 2013 15:54 |
|
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 #39659 is a reply to message #39627] |
Mon, 15 April 2013 18:42 |
|
mirek
Messages: 13984 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 |
Novo
Messages: 1362 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 |
|
mirek
Messages: 13984 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 |
Novo
Messages: 1362 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.
Regards,
Novo
|
|
|
Goto Forum:
Current Time: Wed Jun 05 21:18:27 CEST 2024
Total time taken to generate the page: 0.01669 seconds
|