各コンポーネントで計算を行った後、多くの STL ベクトルを画面に出力する必要があるプログラムがあります。だから私はこのような関数を作成しようとしました:
template <typename a>
void printWith(vector<a> foo, a func(a)){
for_each(foo.begin(), foo.end(), [func](a x){cout << func(x) << " "; });
}
そして、次のように使用します。
int main(){
vector<int> foo(4,0);
printWith(foo, [](int x) {return x + 1;});
return 0;
}
printWith
残念ながら、呼び出しの中に入れたラムダ式の型に関するコンパイル エラーが発生しました。
g++ -std=gnu++0x -Wall -c vectest.cpp -o vectest.o
vectest.cpp: In function ‘int main()’:
vectest.cpp:16:41: error: no matching function for call to ‘printWith(std::vector<int>&, main()::<lambda(int)>)’
vectest.cpp:10:6: note: candidate is: void printWith()
make: *** [vectest.o] Error 1
もちろん、私がそうするなら:
int sumOne(int x) {return x+1;}
その後printWith(foo, sumOne);
、意図したとおりに動作します。ラムダ式の型は、推論された戻り値の型を持つ関数の型になると思いました。また、通常の関数に適合できる場所ならどこにでもラムダを適合できると考えました。どうすればこれを機能させることができますか?