deducing Template Tuple Parameters
Im, Jihyuk
icedac at g-nospam-mail.com
Tue Jan 29 04:46:43 PST 2008
oops for failed text format :0
I saw the Curry example in http://www.digitalmars.com/d/2.0/template.html
"source.1"
[code]
import std.stdio;
/* R is return type
* A is first argument type
* U is TypeTuple of rest of argument types
*/
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);
writefln("%d", plus_two(6, 8)); // prints 16
}
[/code]
But this example use gc instead of manual delete (or RAII), I try to make it
return functor.
"source.2"
[code]
template Curry( R, A, U... )
{
class Curried
{
public:
alias R delegate ( A, U ) Source;
Source src_;
A arg_;
this( Source src, A arg )
{
src_ = src; arg_ = arg;
writefln( "Curried ctor" );
}
~this()
{ writefln( "Curried dtor" ); }
R opCall( U u )
{ return src_( arg_, u ); }
}
Curried Curry( R delegate(A, U) dg, A arg )
{
return new Curried( dg, arg );
}
}
void CurryTest()
{
int sum( int a, int b, int c ) { return a+b+c; }
writefln( "start of curry block" );
{
scope auto p = Curry( &sum, 1 );
writefln( "%d", p( 2, 3 ) ); // == 6
} // automatically p is deleted from here
writefln( "end of curry block" );
}
[/code]
but this code emits
D:\_work\java_flash\test\test.d(206): template test.Curry(R,A,U...) is not a
function template
D:\_work\java_flash\test\test.d(239): template test.Curry(R,A,U...) cannot
deduce template function from argument types (int delegate(int a, int b, int
c),int)
it cant be deduce? actually, i modifed that code to this for making compiler
happy
"source.3"
[code]
class Bind1stFunctor(R, A, U...)
{
public:
alias R delegate ( A, U ) Source;
Source src_;
A arg_;
this( Source src, A arg )
{
src_ = src;
arg_ = arg;
writefln( "Bind1stFunctor ctor" );
}
~this()
{
writefln( "Bind1stFunctor dtor" );
}
R opCall( U u )
{
return src_( arg_, u );
}
}
Bind1stFunctor!(R, A, U ) Curry1st(R, A, U...)(R delegate(A, U) dg, A arg)
{
return new Bind1stFunctor!(R,A,U)( dg, arg );
}
[/code]
can you tell me what's wrong with the second source? I just wanna make it as
one template scope like
soruce.2
---
http://www.xecode.com/blog2
More information about the Digitalmars-d-learn
mailing list