C++ Logo

liaison

Advanced search

Re: [wg14/wg21 liaison] nullptr + 0

From: Thomas Köppe <tkoeppe_at_[hidden]>
Date: Thu, 26 Oct 2023 14:06:33 +0100
On that note, a pet peeve of mine is when people say "nullptr" when they
mean to say "null pointer". I have no idea what gives them this idea (did
we pick a terrible name?), but even experts and committee members do this.
I regularly point out that nullptr is neither null nor a pointer. It sounds
like this may be exactly the kind of confusion here?

Allowing + 0 on a null pointer has the pleasant ramification that [p, p +
N) is a valid range even when p is null and N is 0, which is quite handy in
C++.

On Thu, 26 Oct 2023 at 14:00, JeanHeyd Meneide via Liaison <
liaison_at_[hidden]> wrote:

> On Thu, Oct 26, 2023 at 8:41 AM Jens Maurer via Liaison <
> liaison_at_[hidden]> wrote:
>
>>
>> On 26/10/2023 14.29, Robert Seacord via Liaison wrote:
>> >
>> > Does anyone know why |nullptr + 0| is defined in C++ but not C?
>>
>> What makes you believe "nullptr + 0" to be well-formed in C++?
>>
>> At least two compilers (gcc, clang) agree it's not:
>>
>> x.cc:4:22: error: invalid operands of types ‘std::nullptr_t’ and ‘int’ to
>> binary ‘operator+’
>> 4 | void * p = nullptr + 0;
>> | ~~~~~~~~^~~
>>
>
> Having had this conversation in C before, I'm going to presume he means
> not the literal null pointer but the clause in [expr.add] ¶4.1 that
> explicits exempts it from undefined behavior when used on any pointer whose
> value is the null pointer with 0:
>
> When an expression J that has integral type is added to or subtracted
> from an expression P of pointer type, the result has the type of P:
> — If P evaluates to a null pointer value and J evaluates to 0, the result
> is a null pointer value.
> …
>
> This is indeed different from C, where there is no carve out for this
> case in particular.
>
> Sincerely,
> JeanHeyd
> _______________________________________________
> Liaison mailing list
> Liaison_at_[hidden]
> Subscription: https://lists.isocpp.org/mailman/listinfo.cgi/liaison
> Link to this post: http://lists.isocpp.org/liaison/2023/10/1324.php
>

Received on 2023-10-26 13:06:48