-1

I don't know how to call function call.

It's a templated class, with a templated function call. But how can I use this code?

#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'
Constructor
  • 7,273
  • 2
  • 24
  • 66

1 Answers1

0

You can't pass a a function pointer to std::function like that ( see this question )

Change it to something like this :

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.); //!
}

ideone

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.); //!
}
Community
  • 1
  • 1
uchar
  • 2,552
  • 4
  • 29
  • 50