Definition of double exclamation?

I understand what a double exclamation mark does (or, I think, I understand), but I'm not sure how it is defined on a random object. For example, in the code snippet below:

Assignment *a; if (!getAssignment(query, a)) return false; hasSolution = !!a; if (!a) return true; 

How do you know what value will lead to a double exclamation point? In other words, is it always converted to true? false? or can you define behavior for it, for example, execute a method to determine the result (how does the object know how to act in this situation)? I got a little confused in this piece of code due to all of these exclamation events. Any explanation is appreciated.

I hope that I was clean and grateful.

+7
source share
7 answers

a is a pointer. In C ++, nullptr is defined as an invalid pointer. !pointer turns the nullptr pointer to true and the non- nullptr to false . !boolean turns true to false and false to true . He will always work.

!(!a) is a useful way to think about it.

+13
source

Do not think that this is a β€œdouble exclamation mark”, think of it as two separate operators, one of which works according to the result of the other.

For all primitive types, it "works." !a equivalent to a == 0 , therefore !!a equivalent to !(a == 0) , which in turn is equivalent to a != 0 .

For custom types, it will not compile unless they overload operator ! . But, obviously, in this case, the behavior can be almost any.

+6
source

!! is not the only token in C ++ and just decides to use the operator ! twice.

Since a is a pointer, not an object of type class ! cannot be overloaded. It is defined as return true if a is a null pointer and false otherwise.

The second application ! just denies the result of the first ! .

The expression !!a equivalent to a != 0 .

+2
source

The code is terribly complicated. Actually, you want to check if the getAssigment method getAssigment successful and whether the assigned pointer is non-zero.

The Codex verifies that, although confusing, it uses weak typing, rather than trying to encompass explicit and strong typing. As a result, this is not idiomatic C ++, but harder to understand than necessary.

In particular, do not use !!a in C ++. This is an established idiom in weakly typed languages ​​such as JavaScript to force value to a boolean type. But in C ++ this is usually not used.

It is unclear what the code with hasSolution does is not defined or not used. However, I suspect the code should be equivalent to the following:

 Assignment *a; return getAssignment(query, a) and a == nullptr; 

(Before C ++ 11, you need to write 0 instead of nullptr .)

However, this code still shows a poor design: why is a passed by reference? Why is this not a return value? Worse, a never used, therefore unnecessary. If a really is not needed, it should be completely eliminated. If necessary, this should be a return value. In other words, the getAssignment prototype should be as follows:

 Assignment* getAssignment(the_type_of_query query); 

And it should be used simply as follows:

 Assignment* a = getAssignment(query); 

Also, I suspect that this code actually assigns memory ownership to raw pointer a . This is very discouraged in modern C ++. Either use pointers or a smart pointer.

+1
source
 bool result = true; result = !!result; // result = true, eg !result is false, !!result is !false. 
0
source

Not "!!" operator, so the statement is equivalent to:

hasSolution =! (! a);

So, first the operator! () Is called in the expression "a", then another operator! () Is called on the result. In the case of our code, "a" is a pointer to an Assignement. C ++ defines a special case of using the operator! () In a pointer type: it returns a bool that is true if the pointer is null and false otherwise. In short, this is the same expression (a == 0). Calling the operator! () On the result (! A), which is bool, simply returns the result, that is, returns true if (! A) is false and false if (! A) is true.

So in conclusion, (!! a) returns the same result as (a! = 0). This is because "a" is a pointer.

0
source

The easiest way to remember double negation !! a - narrow a! = 0 to 1 and a == 0 to 0. Which is in a boolean context (i.e. C ++) true or false.

0
source

All Articles