• Advertisement
Sign in to follow this  

Double not operator? (!!)

This topic is 3387 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I saw some code today that used two exclamation marks together, !! . I don't really want to call it a double 'not' operator because it didn't appear to be doing logic in the way a 'not' operator would. Unfortunately I don't have a code sample, I just remembered seeing it and thought I'd ask.

Share this post


Link to post
Share on other sites
Advertisement
It's the "not-not idiom", or something equally descriptive; it is one way to coerce a non-boolean into one:

class some_type
{
bool b;
public:
some_type() : b(true) { }
};

some_type st;

if (st) // syntax error, 'st' cannot be used in a boolean expression
{
}


versus:

class some_type
{
bool b;
public:
some_type() : b(true) { }

bool operator!() const { return b; }
};

some_type st;

if (!!st) // no complaint
{
}

Share this post


Link to post
Share on other sites
That is silly - or possibly worse, stupid.

a) write a cast operator (some_type can be castable to bool)

b) write a method that returns a bool ... and call the method.

examples:

if((bool)truthObject)
{
}

if(someObject.is_true())
{
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Xai
a) write a cast operator (some_type can be castable to bool)

if((bool)truthObject)
{
}


If you implement a cast operator, you don't need the explicit cast.


class some_type
{
bool b;

public:
some_type() : b(true) {}

operator bool() const { return b; }
};

int main()
{
some_type foo;

if(foo)
{
// Works
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Xai
That is silly - or possibly worse, stupid.
There's an article somewhere highlighting all the different ways to 'convert' to a bool along with their associated benefits or hidden and horrific pitfalls, I can't find the link though which is a shame. I believe the benefit of the not-not idiom is that it's an explicit coercion.

Quote:
a) write a cast operator (some_type can be castable to bool)
Another option is to overload the casting to void* operator, the SC++L streams use this method; it avoids some of the more serious pitfalls of casting to bool directly.

Share this post


Link to post
Share on other sites
Quote:
Original post by dmatter
There's an article somewhere highlighting all the different ways to 'convert' to a bool along with their associated benefits or hidden and horrific pitfalls, I can't find the link though which is a shame. I believe the benefit of the not-not idiom is that it's an explicit coercion.

Could this be the one you are referring to? ...if so, my google-fu isn't that bad after all :)
The Safe Bool Idiom [via]

Share this post


Link to post
Share on other sites
Quote:
Original post by sirGustav
Quote:
Original post by dmatter
There's an article somewhere highlighting all the different ways to 'convert' to a bool along with their associated benefits or hidden and horrific pitfalls, I can't find the link though which is a shame. I believe the benefit of the not-not idiom is that it's an explicit coercion.

Could this be the one you are referring to? ...if so, my google-fu isn't that bad after all :)
The Safe Bool Idiom [via]
That's it [smile]
A quick scan through reveals its name to actually be 'the double-bang trick', although to be honest I prefer my name for it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Xai
That is silly - or possibly worse, stupid.


I think this idiom is actually trying to be a bit too clever.

Quote:
Original post by dmatter
I believe the benefit of the not-not idiom is that it's an explicit coercion.


I can't think of anything more explicit than:


if(someObject.is_true())
{
}



You can replace is_true with is_valid or some other helpfully named function which actually describes what it is you are trying to achieve. !! may be explicit, but it just produces confusing code.

Share this post


Link to post
Share on other sites
How about this:

[source lang=cpp]
int count=0;
while(<condition>)
{
ClassType *ptr=someFunctionForReadingOrGettingThatClass();
count+=!!ptr;
//rest of code
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement