On 22/05/2019 16:39, Jonathan Wakely wrote:
On Wed, 22 May 2019 at 15:28, David Brown wrote:
On 22/05/2019 16:12, Jonny Grant wrote:
Hello
This is a little test case. It is not real code.
We used gcc-8 to get some warnings, but on the godbolt trunk gcc doesn't
those warnings with this little test case. Am I missing something
obvious? Is this an issue?
The warning is for treating 0 as a null pointer, but you are not doing
that - you are using NULL as a null pointer.
Right.
The whole point of nullptr is to have a way to give a null pointer that
matches function overloads of pointer type rather than overloads of
integer type:
int foo(void*) { return 1; }
int foo(int) { return 2; }
Calling "foo(0)" will give you 2, which is not the result you want if
you are wanting a null pointer. Calling "foo(nullptr)" will give you 1,
which is the sensible result. Calling "foo(NULL)" will give you an
error about ambiguous overloads.
That's only because NULL is defined as __null, which is equivalent to
0L. It could equally be defined as 0 and then would be identical to
foo(0).
But the point is that "NULL" is not an integer literal (even if it
expands to one), it's a name for a null pointer. It's not as good a
name as "nullptr" but it's still not a literal zero.
In that case, I personally would have liked the warning to trigger here
if NULL is defined in a way that matches an "int" overload without any
warning or error. Clearly if a programmer writes "foo(NULL)" here,
he/she expects the pointer overload to win over the int overload. If
the rules of the language disagree, then a warning would be appropriate.
mvh.,
David
That is perhaps not great, but it will
certainly avoid a quite error in the code. So there is no need for this
warning to trigger on the use of NULL for a null pointer.
(This is just my opinion as a user.)