move an argument tuple

Atila Neves atila.neves at gmail.com
Thu Dec 7 17:57:20 UTC 2017


On Thursday, 7 December 2017 at 08:19:25 UTC, Shachar Shemesh 
wrote:
> Hi everybody,
>
> I'm trying to have a templated function like so:
>
>     void func(alias F)(Parameters!F args) {
>
> At some point in func, I want to call F with args:
>    F(args);
>
> This does not work if one of args is a struct with copying 
> disabled. That's fine. What I'd like to do is to move it into F:
>    F( move(args) );
>
> Except that only works if args.length==1. If args.length==2, it 
> thinks I'm calling the move overload that moves from the first 
> argument to the second, and then it complains that F cannot be 
> called with void argument.
>
> Things that also don't work:
>   F( moveAll(args) );
> That one accepts two ranges and expects to copy between them.
>
> I guess I can put up a mixin that expands to:
>   F( move(args[0]), move(args[1]) );
>
> I'd really like a cleaner solution if one is possible.
>
> Thanks,
> Shachar

import std.traits: Parameters;
import std.stdio: writeln;
import std.functional: forward;

struct Foo {
     int i;
     @disable this(this);
}

auto add(Foo f, Foo g) {
     return f.i + g.i;
}

void main() {
     func!add(Foo(1), Foo(2));
}

void func(alias F)(Parameters!F args) {
     writeln(F(forward!args));
}



More information about the Digitalmars-d mailing list