C++ Logo


Advanced search

Re: Ptr proposal: looking for feedback

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Fri, 17 Jul 2020 17:02:18 +0300
On Fri, 17 Jul 2020 at 04:35, Jefferson Carpenter via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> > If `func` doesn't take ownership, why does it take a `ptr<T>` instead
> > of a `T*`? Can you give an example where using `ptr<T>` as an argument
> > to a function that doesn't take ownership is preferable to using `T*`?
> > I guess that would make it clearer what you're trying to accomplish.
> >
> > Best
> > Manuel
> >
> `func` takes a ptr because it is to take ownership if possible! Taking
> a `ptr<T>` instead of a `T*` indicates that the function /can/ take
> ownership.
> Compare the previous example with
> func(make_ptr<Foo>());
> Here, func actually takes an owning pointer. In this case, as soon as
> the ptr is nulled out somewhere in the execution of func, the instance
> of Foo is deleted. This excludes being nulled out due to std::move - in
> that case ownership is handed off again.

Another way to look at it is that func doesn't take ownership nor not
take it (unless
it explicitly moves from the parameter in its definition); func just
uses the parameter,
and the lifetime of the parameter, and the cleanup at the end of it,
does the rest.
Thus there's not really any confusion about what func does wrt.
ownership, because
func actually doesn't do anything ownership-wise, all of that happens
in the initialization
of the parameter.

Received on 2020-07-17 09:05:48