最後のパラメーターをバインドしながら、std ::function<>オブジェクトを変換する簡単な関数を作成しようとしています。それは私が持っているものです:
template<typename R, typename Bind, typename ...Args> std::function<R (Args...)> bindParameter (std::function<R (Args..., Bind)> f, Bind b)
{
return [f, b] (Args... args) -> R { return f (args..., b); };
}
そして、それが私がそれを使いたい方法です:
int blub (int a, int b)
{
return a * b;
}
// ...
int main ()
{
std::function<int (int, int)> f1 (blub);
// doesn't work
std::function<int (int)> f2 = bindParameter (f1, 21);
// works
std::function<int (int)> f3 = bindParameter<int, int, int> (f1, 21);
return f2 (2);
}
...この例では、main関数は42を返す必要があります。問題は、gcc(4.6)がテンプレートパラメータのタイプを正しく推測していないように見えるため、最初のバージョンでは次のエラーが発生します。
test.cpp:35:58: error: no matching function for call to 'bindParameter(std::function<int(int, int)>&, int)'
test.cpp:35:58: note: candidate is:
test.cpp:21:82: note: template<class R, class Bind, class ... Args> std::function<R(Args ...)> bindParameter(std::function<R(Args ..., Bind)>, Bind)
しかし、私の意見では、パラメータは明らかです。または、この種の型推論は標準でカバーされていないか、gccでまだ実装されていませんか?