c++ - Calling one templated function isn't working -
c++ - Calling one templated function isn't working -
this question has reply here:
isn't template argument (the signature) of std::function part of type? 4 answersi don't know how phone call function call
.
it's templated class, templated function call. how can utilize 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) { homecoming 0; } }; int f(double a, double b) { homecoming (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...)' : not deduce template argument 'overloaded function type' 'overloaded function type' [ result=int ] : see declaration of 'imp<int>::call'
you can't pass a function pointer
std::function
( see question )
change :
template <typename result> class imp { public: template <class func,typename ...args> int call(func f, args... args) { homecoming 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) { homecoming 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.); //! }
c++ templates c++11 variadic-templates std-function
Comments
Post a Comment