Skip to content

Instantly share code, notes, and snippets.

@zwimer
Created November 12, 2024 14:56
Show Gist options
  • Save zwimer/9b95d756b23ef0171cdd4c8d3edefeb2 to your computer and use it in GitHub Desktop.
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!
#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