mercredi 13 mai 2015

Idiomatic way to compare to nullptr: if(p) vs if (p != nullptr) [on hold]

What is the idiomatic way of comparing a pointer to nullptr?

if (ptr) ...
if (!ptr) ...

or

if (ptr != nullptr) ...

I know both are equivalent, but which is preferable?

Personally I find the first version easier to read. But with the implicit conversion from the 0 literal to nullptr no longer being considered idiomatic (say for initialization), I wonder if the same is true for the implicit conversion of a pointer to bool.

Is there any reason I should prefer the second version?

Aucun commentaire:

Enregistrer un commentaire