C++ Logo

std-proposals

Advanced search

Re: [std-proposals] p3039r1 proxy temporary objects

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Fri, 20 Feb 2026 14:04:03 -0500
On Fri, Feb 20, 2026 at 1:39 PM Marcin Jaczewski via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Paper suggests rewriting `ptr->meber` by `(*ptr).member`.
>
> But this creates an interesting case, new `->` created this way will
> have capabilities impossible compared to manually written `->`.
>
> Consider example:
> ```
> const auto& foo = ptr->member;
> ```
> and `ptr` that has a `*` returning proxy value.
> With this rewrite, the lifetime of the proxy object will be expanded
> to the lifetime of the `foo` reference.
> Using user defined `->` you can't in anyway do this as this operator
> is required to return pointer and not value (as it will call nested of `->`).
>
> This is expected behavior? If yes then maybe we should
> discourage of further use of user defined `->` operator?

I think this is fine actually.

`foo()->member` either uses the `operator->` or uses the rewritten
version from `operator*`.

If it uses an existing `operator->`, then there's no problem.
`operator->` cannot return a temporary; it must return a pointer. So
the lifetime of the `->member` part will be based on the lifetime of
the object being pointed to.

If it's rewritten, and `operator*` returns a proxy object, then
extending the lifetime of the proxy temporary is the right thing to
do.

So there aren't cases that would break the expectations of prior code,
since no prior code could assume that `->` dealt in proxy objects.

Received on 2026-02-20 19:04:15