Using Boost Bind with a Boost Unique Pointer and Boost Function I am receiving linker errors depending on how I pass a callback to the receiving function.
If I create a Boost Function member variable by binding a callback containing a boost unique pointer param and pass this onto the receiving function, this results in linker errors when attempting to use the unique pointer when the callback is invoked.
If I perform the bind in place when calling the receiving function I do not get the linker errors and the code behaves as expected.
Sample code:
class test
{
public:
test() : callback_(boost::bind(&test::callback, this, _1, _2))
void start()
{
// using boost function pointer,
// this fails with linker errors
accept(callback_); // (Method 1)
// using in place bind
// this is OK
accept(boost::bind(&test::callback, this, _1, _2)); // (Method 2)
}
void callback(BOOST_RV_REF(boost::movelib::unique_ptr<message>) message,
int version)
{
// attempting to use message if implemented as (Method 1) will result in linker errors
message->get_body(); // If I comment out this line then both methods compile and link???
}
boost::function
< void ( BOOST_RV_REF(boost::movelib::unique_ptr < message >) message,
int version) > callback_;
};
class callback_tester
{
callback_tester(){};
void accept(boost::function
< void ( BOOST_RV_REF(boost::movelib::unique_ptr < message >) message,
int version) callback)
{
// Assignment to local member variable is fine here so we
// can invoke the callback at a later stage.
test_callback_ = callback;
test_callback_(boost::move(message_), version_);
}
// define handler to store and invoke test callback
boost::function
< void ( BOOST_RV_REF(boost::movelib::unique_ptr < message >) message,
int version) > test_callback_;
boost::movelib::unique_ptr<message> message_;
int version_;
};
Some of the linker errors are as follows:
Error: symbol `_ZN5boost8functionIFvRKNS_6system10error_codeERNS_2rvINS_7movelib10unique_ptrIN5cayan3hal7network10tcp_socketENS6_14default_deleteISB_EEEEEEEED2Ev' is already defined
Error: symbol `_ZN5boost9function2IvRKNS_6system10error_codeERNS_2rvINS_7movelib10unique_ptrIN5cayan3hal7network10tcp_socketENS6_14default_deleteISB_EEEEEEED2Ev' is already defined
Error: symbol `_ZNSt15binary_functionIRKN5boost6system10error_codeERNS0_2rvINS0_7movelib10unique_ptrIN5cayan3hal7network10tcp_socketENS6_14default_deleteISB_EEEEEEvEC2Ev' is already defined
...
Can anyone tell me what the difference in the two methods is and why the linker errors only appear when attempting to access the unique pointer when Method 1 is used?
I have come across some information that the callback should be CopyConstructible to use with boost::function. But if that is true I would have expected both methods to bind and pass a callback containing a unique pointer to fail.