Created
November 12, 2024 14:56
-
-
Save zwimer/9b95d756b23ef0171cdd4c8d3edefeb2 to your computer and use it in GitHub Desktop.
C++ bools define `true` as `value == 1`, not merely `value != 0`; casts are dangerous!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
void f(bool b) { | |
if (b == true) { std::cout << "true\n"; } | |
if (b == false) { std::cout << "false\n"; } | |
} | |
int main() { | |
char c = 2; | |
bool * b = (bool*) &c; | |
f(*b); | |
} | |
/* | |
Without optimizations might print: | |
true | |
false | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment