c++ - How to create a std::function alike wrapper? -
attempted this:
template <class r, class... ts> class myfunction { public: using func_type = r(*)(ts...); myfunction(func_type f) : m_func(f) { } r operator()(ts ... args) { return m_func(args...); } private: func_type m_func; }; int testfn(int a) { std::cout << "value " << a; return 42; } void testing() { myfunction<int(int)> func(testfn); std::cout << "ret " << func(1) << std::endl; }
but fails with:
error c2064: term not evaluate function taking 1 c2091: function returns function c2091: function returns c2664: 'myfunction<int (int),>::myfunction(const myfunction<int (int),> &)' : cannot convert argument 1 'int (__cdecl *)(int)' 'int (__cdecl *(__cdecl *)(void))'
compiler msvc2013.
it should this:
template <typename t> class myfunction; template<typename r, class... ts> class myfunction<r(ts...)> { public: using func_type = r(*)(ts...); myfunction(func_type f) : m_func(f) { } r operator()(ts ... args) { return m_func(args...); } private: func_type m_func; };
myfunction
should specialized function signature type. note: std::function
more complicated.
Comments
Post a Comment