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
    }
}
```

Reply via email to