Even though C++ provides "true" and "false" as boolean literals, it allows using integer literals in places where boolean type is expected. This
can be done through implicit or explicit casting.
In contexts where boolean type is expected, integral literals should be avoided. Using boolean literals instead would make your code more readable
and less error-prone.
Noncompliant code example
void f(){
bool isX = 1; // Noncompliant
bool isY = 0; // Noncompliant
bool ternaryIsX = isX ? 1 : isY; // Noncompliant
bool cCast= (bool)0; // Noncompliant
bool cppCast= static_cast<bool>(1); // Noncompliant
if(1) { // Noncompliant
...
}
}
Compliant solution
void f(){
bool isX = true;
bool isY = false;
bool ternaryIsX = isX ? true : isY;
bool cCast= false;
bool cppCast= true;
if(true) {
...
}
}