template ref parameter

Jack Applegame japplegame at gmail.com
Wed Nov 21 07:46:22 PST 2012


Ugly C++ like template and mixin magic solution:

struct r(T){ alias T type;}
template str(alias A) if(is(A == r!(A.type))) { const char[] str 
= "ref " ~ A.type.stringof; }
template str(T) { const char[] str = T.stringof; }
template str(T, A...) { const char[] str = str!T ~ ", " ~ str!A; }

struct functor(A...) {
   void opAssign(C)(C callable) { m_fp = callable; }
   void opCall(R...)(auto ref R r) { m_fp(r); }
   private {
     mixin("void function(" ~ str!A ~ ") m_fp;");
   }
}

void foo(ref int a, int b) { a += b; }

void main()
{
   functor!(r!int, int) f;
   f = &foo;
   int a = 1, b = 2;
   f(a, b);
   assert(a == 3);
}



More information about the Digitalmars-d-learn mailing list