ManInTheHood
ManInTheHood

Reputation: 433

C++11 and if(integer)

Is it still valid and good practice in C++11 write if clause like

int i;
//some code
if (i) //some code

or it is preferred to write

if(i != 0)

Upvotes: 3

Views: 253

Answers (2)

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39390

It is well-defined behaviour, so you can use the shorter expression. It might seem cryptic for a non-experienced developer, but regulars shouldn't have problems in understanding it.

It might have some sense to use the longer form when explicitly treating i as a number, and omitting it when it has some other logical meaning.

Upvotes: 4

triclosan
triclosan

Reputation: 5724

It is valid to use integral POD types as a conditional expression.

I do not think there are any requirements in C++11 to use one style over the other.

Thus, choose whichever style you prefer.

Upvotes: 0

Related Questions