Boost logo

Boost :

From: Gavin Lambert (boost_at_[hidden])
Date: 2022-05-29 23:22:47


On 27/05/2022 23:00, Andrey Semashev wrote:
> It *is* a useless warning. The code with NULL is explicit enough and
> portable, so what is this warning about? That the code is not
> C++11-only? I know that, and it's not up to the compiler to tell me that.

The problem is that NULL is defined as a plain 0, and there is potential
ambiguity with comparing a pointer to 0 -- did you actually intend to
compare the pointer itself or were you intending to compare the value
being pointed to? This is obvious when compared to anything other than
0, but 0 gets this special magic treatment due to historic precedent
(which is, as usual with C/C++, misguided other than for backwards
compatibility).

It's actually worse in C++, because C defines NULL as explicitly
pointer-typed, but C++ doesn't (because it introduces new warnings about
comparing void pointers with other pointers) -- but this also means at
the compiler level it can't tell if you did actually write NULL or 0, so
it has even less idea whether it was intended or not. As usual,
backwards compatibility is a comedy of errors.

Strongly suggesting (but not requiring) that all newly-written code
should use nullptr rather than NULL or 0 is the correct choice. The
only reason you might not do so is for C++03 compatibility -- but newly
written code really shouldn't be trying to do that either any more. The
language has moved on, and codebases should too.

I support use of BOOST_NULLPTR for compatibility of old codebases, however.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk