Вызов одной шаблонной функции не работает

Я не знаю, как вызвать функцию call.

Это шаблонный класс с шаблонным вызовом функции. Но как я могу использовать этот код?

#include <iostream>
#include <conio.h>
#include <functional>

template <typename Result> class Imp {
    template <typename ...Args> int call(std::function<Result(Args...)> func, Args... args) {
        return 0;
    }
};

int f(double a, double b) {
    return (int)a+b;
}

int main() {
    Imp<int> a;
    a.call<double, double>(f, 1., 1.); //!
}

error C2784: 'int Imp<int>::call(std::function<Result(Args...)>,Args...)' : could not deduce template argument for 'overloaded function type' from 'overloaded function type'
      with
      [
          Result=int
      ]
       : see declaration of 'Imp<int>::call'

person user3761373    schedule 20.06.2014    source источник
comment
почему все минусы?   -  person sp2danny    schedule 21.06.2014
comment
@sp2danny Я подозреваю, что выбор английских слов придал этому вопросу определенный запах. Исправление сейчас...   -  person Drew Dormann    schedule 21.06.2014


Ответы (1)


Вы не можете передать aa function pointer в std::function таким образом (см. этот question< /а> )

Измените его на что-то вроде этого:

template <typename Result> class Imp {
public:
    template <class Func,typename ...Args> int call(Func f, Args... args) {
        return 0;
    }
};
int f(double a, double b) {return (int)a+b;}
int main() {
    Imp<int> a;
    a.call(f, 1., 1.); //!
}

идея

Or :

#include <functional>
template <typename Result> class Imp {
public:
    template <typename ...Args> int call(std::function<Result(Args...)> f, Args... args) {
        return 0;
    }
};
int f(double a, double b) {return (int)a+b;}
int main() {
    Imp<int> a;
    a.call(std::function<int(double,double)>(f), 1., 1.); //!
}
person uchar    schedule 20.06.2014
comment
Вы можете преобразовать функцию в std::function. Вы ссылаетесь на вопрос о приведении функции-члена. - person Drew Dormann; 21.06.2014