C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Automatic perfect forwarding is possible and not too complicated

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Thu, 10 Apr 2025 14:18:45 +0300
On Thu, 10 Apr 2025 at 14:14, Jan Schultke via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> I don't think it's worth pursuing this if it cannot work in the
> general case, and it really cannot.
>
> For example, the last mention of a variable could be within the body
> of a loop. In that case, it can be used multiple times with only one
> syntactical appearance. However, there are also idioms involving do {
> ... } while(false) loops, so just because a variable appears in a loop
> body doesn't mean that it's used multiple times.
>
> Another obvious problem with automatic forwarding is that some code
> intentionally copies ranges. For example, I might want to provide
> strong exception guarantees, and cannot do that when moving a range
> throws. In that event, I may copy it, even though the function
> template takes a forwarding reference.

And some code intentionally copies e.g. shared_ptrs to bump the refcount.

> There are probably many other edge cases where existing code would
> either not benefit or be negatively impacted. It's a pretty complex
> language feature, and you're getting very little bang for your buck
> here. To resolve some of the cases involving loops and if statements,
> you need something bordering on a borrow checker; all that just to not
> write std::forward in a few places? It's not like std::forward is
> something you need all the time: only in templates, and only if they
> take forward references.

Agreed on the bang-for-buck problem, and that coupled with the part where this
idea doesn't just remove hard-to-find bugs but introduces new ones,
the idea doesn't
seem feasible.

Received on 2025-04-10 11:19:00