Problem with templates

Sean Reque seanthenewt at yahoo.com
Mon Jul 7 10:04:44 PDT 2008


> It can't.  Delegates and functions currently have different calling 
> conventions and the compiler cannot automatically convert one to the other. 
> Was this just intuition or did you read that it would somewhere?


Take this example:

R delegate(U) Curry(R, A, U...)(R delegate(A, U) dg, A arg)
{
    struct Foo
    {
	typeof(dg) dg_m;
	typeof(arg) arg_m;

	R bar(U u)
	{
	    return dg_m(arg_m, u);
	}
    }

    Foo* f = new Foo;
    f.dg_m = dg;
    f.arg_m = arg;
    return &f.bar;
}

void main()
{
    int plus(int x, int y, int z)
    {
	return x + y + z;
    }

    auto plus_two = Curry(&plus, 2);

Notice how the function Curry accepts a delegate, but a function pointer is actually passed in. I have personally re-written this function to take advantage of D2 closures and it worked perfectly fine.


> Why can't this be done with a static function?  Or rather, I'm not seeing 
> how using a delegate makes what you want to do easier. 
> 

You know, I think you are write and that a static function would work. I would just instantiate templates for every function I wanted to create a new version of.  I don't think I quite understood how it worked at first. And if I really needed a delegate, I could easily wrap the invocation of a specific template instantation inside a delegate call and use that.

Unfortunately, neither your compose or my compose is working for me right now :(.



More information about the Digitalmars-d-learn mailing list