Abs
Abs

Reputation: 165

Why does passing a non-static member function cause compilation errors?

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:

Upvotes: 2

Views: 157

Answers (1)

SirGuy
SirGuy

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

Related Questions