1

I am trying to pass a function pointer using boost::bind.

void
Class::ThreadFunction(Type(*callbackFunc)(message_type::ptr&))
{
}

boost::shared_ptr<boost::thread>
Class::Init(Type(*callbackFunc)(message_type::ptr&))
{
    return boost::shared_ptr<boost::thread> (
        new boost::thread(boost::bind(&Class::ThreadFunction, callbackFunc))
        );
}

I get the following errors:

1>C:\dev\sapphire\boost_1_46_1\boost/bind/mem_fn.hpp(362) : warning C4180: qualifier applied to function type has no meaning; ignored
1>C:\dev\sapphire\boost_1_46_1\boost/bind/mem_fn.hpp(333) : error C2296: '->*' : illegal, left operand has type 'Type (__cdecl **)(message_type::ptr &)'

However, I was able to change to the following, it works fine:

void
ThreadFunction(Type(*callbackFunc)(message_type::ptr&))
{
}

boost::shared_ptr<boost::thread>
Class::Init(Type(*callbackFunc)(message_type::ptr&))
{
    return boost::shared_ptr<boost::thread> (
        new boost::thread(boost::bind(&ThreadFunction, callbackFunc))
        );
}

Why do I get those errors if I declare the method in the Class class?

2 Answers 2

3

When you bind a non-static member function, you need to provide the this pointer that will be used. If you don't want the function associated with a particular instance of Class, you should make the function static.

struct Class {
    void foo(int) { }
    static void bar(int) { }
};

std::bind(&Class::foo, 5); // Illegal, what instance of Class is foo being called
                           // on?

Class myClass;
std::bind(&Class::foo, &myClass, 5); // Legal, foo is being called on myClass.

std::bind(&Class::bar, 5); // Legal, bar is static, so no this pointer is needed.
Sign up to request clarification or add additional context in comments.

1 Comment

+1, I forgot to mention alternatives to using a member function.
2

Because you need to bind an instance of Class as well. Read the Boost documentation.

I think you need this:

boost::thread(boost::bind(
    &Class::ThreadFunction, &someClassInstance, _1), 
    callbackFunc);

Note: above code snippet is off the top of my head. I think it's correct though.

1 Comment

Close enough, but the placeholders are in an unnamed namespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.