我正在尝试创建一个在从串行端口读取的专用线程中运行的函数。但是我无法将连接传递给线程。我将代码简化为以下示例:
#include <thread>
#include <boost/asio.hpp>
template <typename Port>
void serial_read( Port& port) {}
int main()
{
boost::asio::io_service serial_io;
boost::asio::serial_port port( serial_io );
port.open( "/dev/ttyUSB0" );
std::thread s( serial_read<boost::asio::serial_port>, port );
return 0;
}
但是,我收到以下错误消息:
/usr/include/c++/4.7/tuple:128:25: error: use of deleted function ‘boost::asio::basic_serial_port<>::basic_serial_port(const boost::asio::basic_serial_port<>&)’
In file included from /usr/include/boost/asio.hpp:25:0,
from main.cpp:2:
/usr/include/boost/asio/basic_serial_port.hpp:47:7: note: ‘boost::asio::basic_serial_port<>::basic_serial_port(const boost::asio::basic_serial_port<>&)’ is implicitly declared as deleted because ‘boost::asio::basic_serial_port<>’ declares a move constructor or move assignment operator
错误信息非常清楚。我正在尝试使用一个不存在的对象,因为它由于声明了移动构造函数而被删除。但我仍然不明白这里发生了什么以及如何正确地做到这一点。有人可以启发我吗?