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

Rule:  CFL#011Checked automatically with code checker

Synopsis:The test condition in control statements shall be a non-assignment expression of bool type
Language:C++
Severity Level:9
Category:Control Flow


Description:

Use an explicit comparison to make the test condition in a selection or iteration statement of bool type. However, also see [CFL#017].

The test condition in a selection or iteration statement shall not be an assignment expression. This avoids mistaking assignments for comparisons.

Example:

bool	b = true;
bool	c = b;
if (b)			// right, see Rec. CFL#017

int	i = 123;
int*	i_ptr = &i;
if (i)			// wrong: implicit conversion from int to bool
if (i != 0)		// right: expression has bool type
if (i = 123)		// wrong: probably, (i == 123) was intended
if (i_ptr)		// wrong: implicit conversion of int* to bool
if (i_ptr != NULL)	// right: explicit comparison to null pointer


Literature References:
Ellemtel Rec. 55