C++ Logo

std-proposals

Advanced search

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

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Thu, 10 Apr 2025 13:20:07 +0200
On 10/04/2025 12:07, Frederick Virchanza Gotham via Std-Proposals wrote:
>
> I think it would reduce the possibility of introducing
> very-hard-to-find bugs. I mean let's say Billy The Programmer started
> out by writing the following function:
>
> template<typename T>
> void ProcessNode(T &&arg)
> {
> NormaliseNode(arg);
> DistributeNode(arg);
> AssimilateNode( forward<T>(arg) );
> }
>
> and the Mandy The Programmer came along a year later and added a new line:
>
> template<typename T>
> void ProcessNode(T &&arg)
> {
> NormaliseNode(arg);
> DistributeNode(arg);
> AssimilateNode( forward<T>(arg) );
> TrimMemoryUsage( forward<T>(arg) );
> }

You'd also need a way to stop this automatic forwarding:

template <std::range T>
void f(T &&& arg)
{
   auto b = ranges::begin(arg);
   auto e = ranges::end(arg);

   use(b, e);

   assert(check(arg));
}

Under NDEBUG one doesn't want the call to `end` to actually forward.
In other words, this problem is way more complicated than "just forward
on the last usage".

My 2 c,
--
Giuseppe D'Angelo

Received on 2025-04-10 11:20:11