Using numeric types as boolean is considered antipattern, because it can lead to unexpected behavior.
For example, if you use an integer type as a boolean type in C++, then any non-zero value will be
treated as true
and zero will be treated as false
.
This can lead to bugs that are hard to find. It’s better to use boolean literals instead of numeric types because they make your code more readable and less error-prone.
int main() {
auto x = 1;
if (x) {
// This block will execute because x is non-zero
}
return 0;
}
int main() {
auto x = true;
if (x) {
// This block will execute because x is true
}
return 0;
}