C++ Logo

std-discussion

Advanced search

Re: Is (0) a null pointer constant?

From: Brian Bi <bbi5291_at_[hidden]>
Date: Fri, 21 Jul 2023 12:23:47 -0400
On Fri, Jul 21, 2023, 11:52 AM Julien Villemure-Fréchette via
Std-Discussion <std-discussion_at_[hidden]> wrote:

> > I guess this makes (0) a null pointer constant. I think [conv.ptr] can
> be made clearer by adding "(possibly parenthesized)" to the wording.
>
> I think the intent would be the opposite, and that compilers are actually
> too permissive with respect to [conv.ptr]. Looking at the last sentence in
> [expr.prim.parent] (https://eel.is/c++draft/expr.prim.paren):
>
> "...can be used in exactly the same contexts as those where E can be used,
> and with the same meaning, except as otherwise indicated."
>
> I think the intent of "except as otherwise indicated" encompasses the case
> of null pointer conversion. There are a few context in which a
> parenthesized expression has not the same meaning as the expression. On top
> of my head, I can think of 4 places in the standard where a parenthesized
> expression "(expr)" has not the same meaning as the operand expression
> "expr":
>
> - in "decltype(expr)": if expr is an id expression,
> then it changes the type result type. (This
> case is unfortunate IMO, we really should
> have a distinct unevaluated operator that
> would make this distinction, but that would
> deserve a proposal on its own).
>
> - in null pointer conversion [conv.ptr]: a
> zero valued literal is required.
>
> - in null member pointer conversion
> [conv.mem]: a zero valued literal is required.
>
> - when an object of comparison category type
> is an operand of equality, relational or
> spaceship operator [cmp.categories.pre]:
> the other operand is required to be a zero
> valued literal. See
> https://eel.is/c++draft/cmp.categories#pre-3
>
> But I agree there should be an editorial fix. An informal footnote could
> be added, like
>
> "...for instance, such context include the null pointer conversion
> (including member pointer), decltype operand, and comparison with a
> comparison categories."
>

I think it's better to clarify that the parentheses are allowed in the case
of null pointer constants. Extraneous parentheses are often introduced by
macros, so it gives a better user experience when those parentheses have no
effect. I agree that the case of `decltype` is unfortunate, but we can't
change it, so the best we can do is avoid repeating that mistake.


> and may even specify explicitly that the expression "(0)" is ill formed
> when used as pointer or when comparing with the result of a spaceship
> operator.
>
>
> Julien V.
>
>
> On July 21, 2023 6:09:59 a.m. EDT, "Lénárd Szolnoki via Std-Discussion" <
> std-discussion_at_[hidden]> wrote:
>
>> On Fri, 2023-07-21 at 14:48 +0500, language.lawyer--- via Std-
>> Discussion wrote:
>>
>>> Is a parenthesised integer literal with value zero a null pointer
>>>> constant?
>>>>
>>>> The current wording doesn't seem to allow that. [conv.ptr] says
>>>> that "a
>>>> null pointer constant is an integer literal ([lex.icon]) with value
>>>> zero ...", which should exclude (0).
>>>>
>>>> All of gcc, clang and MSVC accept `void * ptr = (0);`.
>>>>
>>>
>>> https://timsong-cpp.github.io/cppwp/n4868/expr.prim.paren#1.sentence-2
>>> : The parenthesized expression can be used in exactly the same
>>> contexts as those where E can be used, and with the same meaning,
>>> except as otherwise indicated.
>>>
>>
>> Thank you, I guess this makes (0) a null pointer constant. I think
>> [conv.ptr] can be made clearer by adding "(possibly parenthesized)" to
>> the wording. But this is purely editorial.
>>
>> Cheers,
>> Lénárd
>>
>> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>

Received on 2023-07-21 16:24:01