I am trying to write a simple template that I can use for memoization with functions taking a single argument:
#include <map>
template <typename F,typename OUT,typename IN>
OUT memoization(IN in){
static std::map<IN,OUT> memo;
static typename std::map<IN,OUT>::iterator found = memo.find(in);
if (found != memo.end()) { return found->second; }
OUT res = F(in);
memo(in) = res;
return res;
}
double test(double x) { return x*x; }
int main(){
for (int i=0;i<5;i++){
memoization<test,double,double>(i*0.5);
}
}
But i get the error:
error: no matching function for call to 'memoization(double)'
note: candidate is:
note: template OUT memoization(IN)
note: template argument deduction/substitution failed:
Why does this fail to compile?
Actually I dont understand why template argument deduction/substitution is taking place at all when I specify all the template parameters.
I am using gcc version 4.7.2 (no C++11 enabled)
PS: the template has many more errors than I first realized, but I leave it as is...
testis not a type.decltype(test)is. - MadScientist