Я пытаюсь написать простую функцию для преобразования объекта 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);
}
... так что в этом примере основная функция должна возвращать 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?