将Boost绑定与Boost唯一指针和Boost函数一起使用我接收链接器错误取决于我如何将回调传递给接收函数。
如果我通过绑定包含boost唯一指针参数的回调来创建Boost Function成员变量并将其传递给接收函数,则在调用回调时尝试使用唯一指针时会导致链接器错误。
如果我在调用接收函数时执行绑定,则不会出现链接器错误,代码的行为与预期的一样。
示例代码:
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_;
};
一些链接器错误如下:
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
...
有人能告诉我这两种方法的区别是什么以及为什么只有在使用方法1时尝试访问唯一指针时才会出现链接器错误?
我遇到过一些信息,回调应该是CopyConstructible与boost :: function一起使用。但如果这是真的,我会期望两种方法都绑定并传递包含失败的唯一指针的回调。
答案 0 :(得分:0)
message
类必须具有显式声明的删除
至少~message()=默认;