Is it possible to compare two booleans in C ++?

Should the following code snippet work?

bool b1 = true; bool b2 = 1 < 2; if (b1 == b2) { // do something } 

I suspect that not all "truths" are equal.

+4
source share
8 answers

Yes. All truths are equal.

+17
source

Yes, booleans can only store true or false, and you can compare values โ€‹โ€‹for equality.

However, some bad uses bool variables can lead to the behavior of "undefined" and may look like it's not true, but false. For example, reading the value of an uninitialized automatic variable or a direct copy of memory from integers.

Take a look at the following (bad) example:

  bool b1 = true; bool b2 = true; *((char*)&b1) = 3; if( b1 ) cout << "b1 is true" << endl; if( b2 ) cout << "b2 is true" << endl; if (b1 != b2) cout << "b2 is not equal to b1" << endl; 

Visual Studio 9 shows:

b1 is true

b2 true

b2 is not equal to b1

Perhaps because the compiler directly compares the stored values.

+9
source

Yes, as others have said, bools can be compared for equality in C ++. You might be thinking about the things you heard from C. Since C is not of type bool, booleans are represented as integers. In a Boolean context, any nonzero integer is true. However, they may have different bit patterns and therefore not be equal. Thus, a rule in C should not compare "booleans".

Edit: for comments, C99 is of type bool. However, the answer point was to indicate why the idea of โ€‹โ€‹not comparing bools is floating around. It is based on the long history of C, to C99.

+7
source

When you assign an integer value to a logical object (or initialize a logical object with an integral value), it is implicitly converted to true or false standard Boolean conversion (4.12). So, in terms of language, your 1 and 2 disappear without a trace long before you compare. Both of them became the same true . There is no "all truths are equal." There is only one true .

Of course, some compiler might take a "lazy" approach and preserve several different "truths", making sure that they are "all equal" at the time of comparison, etc., but I doubt that this is a reasonable / viable approach.

In other words, in a reasonable implementation, you should expect that not only your comparison will be true, but also a stronger comparison:

 bool b1 = true; bool b2 = 1 < 2; if (memcmp(&b1, &b2, sizeof(bool)) == 0) { /* We should get in here */ } 

This is not guaranteed by the language, but in real life it pretty well describes the physical side of the situation.

+2
source

In C with int , you might have a period (although even there I think this particular sequence will be fine). In C ++, yes, it is safe.

+1
source

In C ++, bool is a native type, with two possible values true and false . All comparisons will go as you expect. All boolean true values โ€‹โ€‹are the same and the same with all false . It is true that not all expressions that you can evaluate with true or false are the same.

In C89, to return as far as I want, any null value (of any pointer or numeric type) is false, and something else is true. This means that true values โ€‹โ€‹are not necessarily equal to each other. 1 and 2 are true values, but 1 != 2 and 1 & 2 are evaluated as 0, which is false.

It is also possible that C89 false values โ€‹โ€‹are not compared with equal ones, although they will be used in all implementations I have ever used. The null pointer value is a constant integral zero assigned to the pointer value. It is possible that the value of the constant 0, other than the value of the pointer, should not be a null pointer (and there were systems in which null pointers were not all bits of 0). Therefore, (void *)0 is the value of the null pointer and therefore false, but int i;...i = 0;...(void *)i may not be the value of the null pointer and therefore not false.

However, on C89, all operations that intend to return a boolean value (e.g. && or == , for example) will return 1 or 0, so (1 == 3) == (4 ==3) .

+1
source

Problems only arise when you use a non-zero value of true and forget that not all non-zeros are equal.

Imagine the following:

You have a keyPressed () function that returns 0 without pressing a key, the key number when you press a key.

You wrote a simple switch in a loop:

 if(keyPressed() && allow) ... 

Now your company introduces normally open triggers in devices, and you need a prefix.

 bool key_switches_on = getPref("KeySwitchesOn"); if((keyPressed() && allow) == key_switches_on) ... 

Then you notice that "allow" is placed incorrectly ...

 if((keyPressed() == key_switches_on) && allow) 

and suddenly only the key number works.

0
source

I read all these answers about why true is true and why two boolean variables can be compared using == or != , Except in rare cases when an integer is forcibly used like bool or some such thing. However, I have the same problem like the original poster. I have two logical variables, each of which is โ€œtrueโ€, but when I compare them, I think that they are not equal. Here is a line of code,

if (angDegDiff > 15 || scaleRatioA > 5 || scaleRatioB < -5 || (isParallel2 != isParallel1)) { return false; }

In my example, angDegDiff = 0 , scaleRatioA = 0 , scaleRatioB = 0 , isParallel2 = true and isParallel1 = true . However, the expression evaluates to true , and the only way to do this is if isParallel2 not equal to isParallel1 .

No fancy methods are used to set the values โ€‹โ€‹of isParallel1 or isParallel2 . Their values โ€‹โ€‹are set using an expression of type _isParallel = true; . Later this value is copied to another variable using an instruction like isParallel1 = geom1->IsParallel(); which is implemented as return _isParallel; .

My conclusion is that, depending on the compiler, two boolean variables cannot be reliably mapped for equality. I am using Microsoft Visual C ++ 2005, Version 8.0.50727.4039.

Epilogue: I replaced the logical comparison in my code with the expression ((isParallel1 && !isParallel2) || (!isParallel1 && isParallel2)) , and now everything works fine.

0
source

Source: https://habr.com/ru/post/1300176/


All Articles