Программирование
- 1 ответ
- 0 вопросов
0
Вклад в тег
module main;
import std.stdio;
import std.traits;
Val AdaptedFunc(alias Func, A, Val)(A a, Val V)
{
alias ReturnType!Func AT;
static assert(__traits(compiles, AT(a,V)), AT.stringof ~ " must have constructor this("~A.stringof~","~Val.stringof~")");
static assert(__traits(compiles, Val(AT(a,V))), Val.stringof ~ " must have constructor this("~AT.stringof~")");
return Val(Func(AT(a,V)));
}
struct Test1
{
string name;
int val;
this(string pName, Test2 test)
{
name = pName;
val = test.val;
}
}
Test1 test1Transform(Test1 test)
{
test.val *= test.val;
return test;
}
struct Test2
{
int val;
this(Test1 pVal)
{
val = pVal.val;
}
}
int main(string[] args)
{
Test2 testA;
testA.val = 2;
Test2 testB = AdaptedFunc!test1Transform("first", testA);
writeln(testB.val); // prints 4
getchar();
return 0;
}