C & C++

C & C++

Made by DeepSource

Found use numeric type as boolean CXX-W2034

Anti-pattern
Minor

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.

Bad practice

int main() {
    auto x = 1;
    if (x) {
        // This block will execute because x is non-zero
    }
    return 0;
}

Recommended

int main() {
    auto x = true;
    if (x) {
        // This block will execute because x is true
    }
    return 0;
}