TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C++ Coding Standard
Search

Rule:  CON#007Checked automatically with code checker

Synopsis:Do not convert implicitly from a boolean type to a non-boolean type, and vice versa.
Language:C++
Severity Level:3
Category:Conversions


Description:

Example:

int i = 5, j = 4, k = 3, l = 2; 

bool b = (i > j);              // right 
int a = (i > j);                 // wrong: implicit conversion from bool to int 
int a = ((i > j) + (k > l)) // wrong: implicit conversion from bool to int

Exception:

Since smart pointers are designed according to the "safe bool idiom" (they behave correctly in boolean context), it is also allowed to use a smart pointer in a boolean context, e.g.

shared_ptr<A> a_sp = someFunctionReturningSharedPtr();
bool my_bool = a_sp;   // right: smart pointers are implicitly of boolean type