forward tuple arg to local variable + dtor

Stanislav Blinov stanislav.blinov at gmail.com
Sat Jan 22 19:01:09 UTC 2022


On Saturday, 22 January 2022 at 18:00:58 UTC, vit wrote:

> I want implement something like this:
> ...

Take by value and make a copy without forwarding:

```d
import std.typecons : Tuple;
import std.meta : allSatisfy;

enum bool isRcPtr(T) = is(T == RcPtr!U, U);

//@safe access to data of multiple ref counted objects:
public auto apply(alias fn, Args...)(Args args)
if (allSatisfy!(isRcPtr, Args)) {

     Tuple!Args params = args; // borrow all

     @property auto ref elm(alias param)()@trusted{
         return param.get();
     }

     return fn(staticMap!(elm, args)); // staticMap over original 
args tuple
}
```

Or make a helper function that takes by value and forward to that:

```d
template apply(alias fn)
{
     private auto impl(Args...)(Args args)
     {
         @property auto ref elm(alias param)()@trusted{
             return param.get();
         }
         return fn(staticMap!(elm, args));
     }

     auto apply(Args...)(auto ref Args args)
     if (allSatisfy!(isRcPtr, Args))
     {
         import core.lifetime : forward;
         return impl(forward!args); // impl takes by value - 
RcPtrs passed by reference will be copied
     }
}
```


More information about the Digitalmars-d-learn mailing list