Reputation: 165
When I try to invoke the below constructor, passing it a static member function I don't get any errors but when I pass it a non-static member function I get a compilations error:
Constructor
template <class callable, class... arguments>
Timer(int after, duration_type duration, bool async, callable&& f, arguments&&... args)
{
std::function<typename std::result_of<callable(arguments...)>::type()>
task(std::bind(std::forward<callable>(f), std::forward<arguments>(args)...));
}
Invokation
Timer timer(252222, duration_type::milliseconds, true, &MotionAnalyser::ObjectGarbageCollector); // Does not work because it does not point to object too.
Timer timer(252222, duration_type::milliseconds, true, std::bind(this, &MotionAnalyser::ObjectGarbageCollector)); //Should work, but does not?!?!
Error
Error C2039 'type': is not a member of 'std::result_of<callable (void)>'
So far I have:
std:function
is used, which turned out to be in
conjunction with a callable type, the invoking object should be a
callable type since I over-rode the ()
operator (based on my
understanding of callable types).std::bind
Upvotes: 2
Views: 157
Reputation: 10770
You have your call to bind
backwards, it takes the callable object first (in this case the pointer to member function) and then the parameters afterwards.
std::bind(&MotionAnalyser::ObjectGarbageCollector, this)
However, looking at the constructor for Timer
you should be able to pass those arguments along, since they get bound anyway:
Timer timer(252222, duration_type::milliseconds, true,
&MotionAnalyser::ObjectGarbageCollector, this);
Upvotes: 2