Date: Fri, 27 Jun 2025 12:37:56 +0100
Jonathan Wakely wrote:
> On Fri, 27 Jun 2025 at 12:03, Phil Endecott via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> Dear Experts,
>>
>> Is there any reason why std::to_address() cannot be used on nullptr,
>> returning nullptr? Should it be extended to do so?
>
>
> Why?
>
> nullptr_t is not a pointer type and not an iterator type. The return value
> is not an address, you cannot use it where a raw pointer is required (most
> uses of to_address that I'm aware of involve dereferencing and/or
> incrementing the pointer).
>
> What generic code do you have where you are dealing with a std::nullptr_t
> value that you need to treat equivalently to an iterator?
I have something that takes a pointer. Passing null is valid and
means "don't know" or similar:
void f(Foo* p);
f(&something); // OK
f(nullptr); // OK
I've added an overload that takes auto and forwards to the original
function using to_address:
void f(auto p) { f(std::to_address(p); }
This makes it possible to pass iterators, etc.
But with this overload present, f(nullptr) fails to compile; the auto
overload matches and tries to call to_address(nullptr).
Of course it is fixable by giving the overload a suitable concept, rather
than just auto.
Still I am curious about the situation. What would break or be
worse if to_address() took nullptr?
Regards, Phil.
> On Fri, 27 Jun 2025 at 12:03, Phil Endecott via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> Dear Experts,
>>
>> Is there any reason why std::to_address() cannot be used on nullptr,
>> returning nullptr? Should it be extended to do so?
>
>
> Why?
>
> nullptr_t is not a pointer type and not an iterator type. The return value
> is not an address, you cannot use it where a raw pointer is required (most
> uses of to_address that I'm aware of involve dereferencing and/or
> incrementing the pointer).
>
> What generic code do you have where you are dealing with a std::nullptr_t
> value that you need to treat equivalently to an iterator?
I have something that takes a pointer. Passing null is valid and
means "don't know" or similar:
void f(Foo* p);
f(&something); // OK
f(nullptr); // OK
I've added an overload that takes auto and forwards to the original
function using to_address:
void f(auto p) { f(std::to_address(p); }
This makes it possible to pass iterators, etc.
But with this overload present, f(nullptr) fails to compile; the auto
overload matches and tries to call to_address(nullptr).
Of course it is fixable by giving the overload a suitable concept, rather
than just auto.
Still I am curious about the situation. What would break or be
worse if to_address() took nullptr?
Regards, Phil.
Received on 2025-06-27 11:37:58