digitalmars.D.learn - Memoizing a templated function
- ixid (49/49) Apr 16 2012 Memoizing a templated version of a function doesn't seem to work,
- bearophile (20/22) Apr 16 2012 A template isn't a function, it's just a recipe to define a
- Andrej Mitrovic (10/12) Apr 16 2012 Instantiate the template first. Either:
Memoizing a templated version of a function doesn't seem to work,
how would I do it properly? For example:
int test(int n)
{
return //Do memoizable stuff
}
T test2(T)(T n)
{
return //Do memoizable stuff
}
void main()
{
n = memoize!test(n); //is fine
n = test2(n); //is fine
n = memoize!test2(n); //gives errors
}
Error 1 Error: template instance ReturnType!(test)
ReturnType!(test) does not match template declaration
ReturnType(func...) if (func.length == 1 &&
isCallable!(func)) z:\Dlang\dmd2\src\phobos\std\functional.d 693
Error 2 Error: ReturnType!(test) is used as a
type z:\Dlang\dmd2\src\phobos\std\functional.d 693
Error 3 Error: template instance ParameterTypeTuple!(test)
ParameterTypeTuple!(test) does not match template declaration
ParameterTypeTuple(func...) if (func.length == 1 &&
isCallable!(func)) z:\Dlang\dmd2\src\phobos\std\functional.d 694
Error 4 Error: ParameterTypeTuple!(test) is used as a
type z:\Dlang\dmd2\src\phobos\std\functional.d 693
Error 5 Error: index is not a type or an
expression z:\Dlang\dmd2\src\phobos\std\functional.d 695
Error 6 Error: template std.typecons.tuple does not match any
function template
declaration z:\Dlang\dmd2\src\phobos\std\functional.d 696
Error 7 Error: template std.typecons.tuple(T...) cannot deduce
template function from argument types
!()(_error_) z:\Dlang\dmd2\src\phobos\std\typecons.d 687
Error 8 Error: template main.test does not match any function
template
declaration z:\Dlang\dmd2\src\phobos\std\functional.d 703
Error 9 Error: template main.test(T) cannot deduce template
function from argument types !()(_error_) C:\Documents and
Settings\Adam.LAPPY\My Documents\Visual Studio
2008\Projects\d\test2\test2\main.d 5
Error 10 Error: template instance std.functional.memoize!(test)
error instantiating C:\Documents and Settings\Adam.LAPPY\My
Documents\Visual Studio 2008\Projects\d\test2\test2\main.d 12
Error 11 Error: forward reference to memoize C:\Documents and
Settings\Adam.LAPPY\My Documents\Visual Studio
2008\Projects\d\test2\test2\main.d 12
Apr 16 2012
ixid:Memoizing a templated version of a function doesn't seem to work,A template isn't a function, it's just a recipe to define a function given one or more compile-time values or types. So memoize works if you instantiate in some way the template: import std.functional; int test(int n) { return n * n; } T test2(T)(T n) { return n * n; } void main() { int n = 5; n = memoize!test(n); // OK n = test2(n); // OK n = memoize!(test2!int)(n); // OK n = memoize!(test2!(typeof(n)))(n); // OK } Bye, bearophile
Apr 16 2012
On 4/17/12, ixid <nuaccount gmail.com> wrote:Memoizing a templated version of a function doesn't seem to work, how would I do it properly?Instantiate the template first. Either: n = memoize!(test2!int)(n); or: alias test2!int itest; n = memoize!itest(n); Actually I'm a little surprised this doesn't work: memoize!(test2(1)); Seems like IFTI should come into play here but doesn't for some reason.. anyone else's thoughts?
Apr 16 2012









"bearophile" <bearophileHUGS lycos.com> 