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 if the implicit conversion from 0literal to is nullptrno longer considered idiomatic (say, for initialization), I wonder if this is true for the implicit conversion of a pointer to bool.
Is there any objective reason to prefer each other?
As an analogy, consider a function void foo(void*). Here the call, foo(nullptr)instead of relying on implicit conversion foo(0), is better because the latter is interrupted after adding overload void foo(int).
Andre source
share