C++ Logo

std-proposals

Advanced search

Re: Ptr proposal: looking for feedback

From: Jefferson Carpenter <jeffersoncarpenter2_at_[hidden]>
Date: Sat, 18 Jul 2020 20:15:53 +0000
Hi Jason

Thanks again for spending your time replying to this thread. Whether to
keep potential cruft out of the standard or to help shape this into a
reasonable proposal, your contributions are appreciated.

Thanks,
Jefferson

On 7/18/2020 3:15 PM, Jason McKesson via Std-Proposals wrote:
> On Sat, Jul 18, 2020 at 8:43 AM Lyberta via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
>>
>> Ville Voutilainen via Std-Proposals:
>>> On Fri, 17 Jul 2020 at 17:40, Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]> wrote:
>>>> But if that were actually the only use-case, then we wouldn't need ptr<T> at all! There's already an idiomatic C++ way of doing that:
>>>> void betterfunc(Foo& f);
>>>> ... betterfunc(local); ... // no ownership
>>>> ... betterfunc(*std::make_unique<Foo>(42)); // construct an owning unique_ptr, and then deallocate it afterward
>>>> If the parameter `Foo& f` were const-qualified, we could even do this:
>>>> ... betterfunc(Foo(42)); ... // construct a Foo and deallocate it afterward, without even using the heap!
>>>
>>> ..but if the parameter of betterfunc is supposed to be nullable, none
>>> of that nonsense works.
>>
>> This is exactly why we need std::optional<T&> in the standard.
>
> Or you can just pass a `T*`. I know some people don't like that idea
> because "ownership semantics are unclear", but they are clear by
> convention: naked pointers have no ownership of what they point to.
> The end.
>
> Or if you absolutely *must* represent such non-ownership, then you can
> use `observer_ptr<T>`.
>

Received on 2020-07-18 15:19:13