Memoizing a templated function

bearophile bearophileHUGS at lycos.com
Mon Apr 16 17:36:38 PDT 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


More information about the Digitalmars-d-learn mailing list