adaptTuple

ある関数funcの引数に、タプルを適用できるようにします。

template adaptTuple(alias func, T...)
ref
adaptTuple
(
X
)
(
ref X arg
)
if (
is(Unqual!X : Tuple!U,
U...
) &&
)

Examples

1 static ref int func1(ref int a, float b, byte c)
2 {
3     return a;
4 }
5 
6 alias adpt1 = adaptTuple!func1;
7 auto t = tuple(4, 1.1f, cast(byte)2);
8 assert(&adpt1(t) == &(t.field[0]));
9 
10 // NG; adpt1の引数がlvalueじゃない(std.forwardのような転送)
11 static assert(!is(typeof({adpt1(tuple(4, 1.1f, cast(byte)2));})));
12 
13 // naryFun & 事前に型指定あり
14 alias adpt2 = adaptTuple!(naryFun!"a", int, float, byte);
15 assert(&adpt2(t) == &(t.field[0])); // forward性
16 assert(adpt2(tuple(4, 1.1f, cast(byte)2)) == t.field[0]);
17 
18 // NG; stringはfloatへ暗黙変換不可能
19 static assert(!is(typeof({adpt2(tuple(1, "foo", cast(byte)2));})));
20 
21 // OK; realはfloatへ暗黙変換可能
22 static assert(is(typeof({adpt2(tuple(1, 1.1L, cast(byte)2));})));
23 assert(adpt2(tuple(1, 1.1L, cast(byte)2)) == 1);
24 
25 const ct = t;
26 static assert(is(typeof(adpt2(ct)) == const));
27 
28 immutable it = t;
29 static assert(is(typeof(adpt2(it)) == immutable));
30 
31 shared st = t;
32 static assert(is(typeof(adpt2(st)) == shared));
33 
34 
35 assert(adaptTuple!(naryFun!"a")(tuple(1, 2, 3)) == 1);
36 assert(adaptTuple!(naryFun!"a", int)(tuple(1)) == 1);

Meta