c++ - Given the following, what type is "function"? -
given following, type "function"?
#include <functional> #include <vector> template<class t> class foo { public: template<typename p, typename q, typename r, typename... args> void attach(p (q::*f)(args...), r p) { auto function = [p, f](args... args) { (*p.*f)(args...); }; listeners.push_back(function); } private: std::vector<std::function<t>> listeners; }; class bar { public: int handler(int x, int y, int z) { return 0; } }; int main(void) { auto foo = foo<int(int, int, int)>(); auto bar = bar(); foo.attach(&bar::handler, &bar); }
for context i'm attempting make lambda calls method on instance, storing lambda collection. push_back fails compile, following error:
xrefwrap(283): error c2440: 'return' : cannot convert 'void' 'int'
i using std::bind make std::function store. apparently may able lambda (and variadic templates, don't need 1 template per arity) far it's defeating me.
when have constructed foo
, explicit template argument supplied auto foo = foo<int(int, int, int)>();
,
int main(void) { auto foo = foo<int(int, int, int)>(); //<<<< here template arguments when constructing foo auto bar = bar(); foo.attach(&bar::handler, &bar); }
where when registering listener function signatures return type void
auto function = [p, f](args... args) { (*p.*f)(args...); };
resolution
so either
- change explicit template argument of foo,
auto foo = foo<void(int, int, int)>();
return void match listener callback. - or ensure listener function returns integer.
return (*p.*f)(args...);
note may intended return status code listener might have missed? second resolution point looks me obvious plausible issue
Comments
Post a Comment