私は単一の引数を取る関数でメモ化のために使用できる簡単なテンプレート記述しようとしています:ここでテンプレート引数の控除/置換が失敗するのはなぜですか?
#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);
}
}
をしかし、私はエラーを取得:
error: no matching function for call to 'memoization(double)'
note: candidate is:
note: template OUT memoization(IN)
note: template argument deduction/substitution failed:
なぜこれはコンパイルに失敗しません?
実際、私はすべてのテンプレートパラメータを指定すると、テンプレート引数の控除/置換がなぜ起こっているのか理解できません。
私はgccのバージョン4.7.2(ないC++ 11が有効になっていない)
PSを使用しています:...テンプレートは、私が最初に実現よりも多くのエラーがありますが、私はそのままそれを残す
'test'はタイプではありません。 'decltype(test)'はです。 – MadScientist