Q:

C++ rely on implicit conversion to bool in conditions?

I found the following rule in a coding standards sheet :

Do not rely on implicit conversion to bool in conditions.
if (ptr)         // wrong
if (ptr != NULL) // ok

How reasonable/usefull is this rule?
How much overload on the compiled code?

A:

In the strictest sense, you can rely on implicit conversions to bool. Backwards compatibility with C demands it.
Thus it becomes a question of code readability. Often the purpose of code standards is to enforce a sameness to the code style, whether you agree with the style or not. If you're looking at someone else's standard and wondering if you should incorporate it into your own, go ahead and debate it - but if it's your company's long-standing rule, learn to live with it.

A:

In most cases, it's not horrible, but it can be more readable if you type exactly what you mean.

A:

That rule puts you in a bind if you ever want to use a class that has an implicit conversion to bool, such as std::istream.  This code reads a word at a time from a file until EOF is reached:
std::ifstream file("foo.txt");
std::string word;

while (file >> word)
{
    // do stuff
}

The stream extraction operator returns a reference to the file stream, which is implicitly converted to bool to indicate whether the stream is still in a good state.  When you reach the end of the file, the test fails.  Your coding standard precludes you from using this common construct.
For pointer types, it's not a big deal.  The compiler will probably produce about the same code for the implicit conversion to bool and the explicit test against NULL.  It's a matter of taste at that point - neither one is "better" in an absolute sense.  The coding standard is simply trying to enforce a consistent style.
With that in mind, you should absolutely follow the coding standard when dealing with built-in types (pointers, ints, etc.).  If you run into a similar situation to the above with a class having a legitimate conversion to bool, I would raise the issue with your teammates.

