Search code examples
c++boostboost-bindboost-coroutine

How to properly use a class member function with boost::coroutine?


I'm currently working with boost::asymmetric_coroutine. Let's say we have an ordinary function in the global namespace:

void foo(boost::coroutines::asymmetric_coroutine<int>::push_type & sink)
{
    //do something
    sink(100);
    //do something else
}

In that case, we can create and execute a coroutine using this function in the following manner:

boost::coroutines::asymmetric_coroutine<void>::pull_type coro{foo};

What I need to do is to use a class member function the same way. As I understand it, boost::bind should be used somehow, however, the following code doesn't work:

class MyClass
{
public:
    void create_coro()
    {
        boost::coroutines::asymmetric_coroutine<int>::pull_type coro
        {
            boost::bind(&MyClass::foo, _1);
        };
    }

private:
    void foo(boost::coroutines::asymmetric_coroutine<int>::push_type & sink)
    {
        //do something
    }
}

The error it raises is

no matching function to call to 'get_pointer(const boost::coroutines::push_coroutine&)'

How do I do it properly?


Solution

  • It should be

        boost::coroutines::asymmetric_coroutine<int>::pull_type coro
        {
            boost::bind(&MyClass::foo, this, _1);
        };
    

    note this, since you use member function.