c++0x lambdas,不让我作为函数 ptr 传递
我目前正在用 C++0x 编写一个程序,我对它还很陌生。
我正在对象之间设置回调并使用 lambda 来匹配类型(就像 boost::bind()
在某种程度上所做的那样)
如果我调用 asio 库中的函数,例如:
socket_.async_read_some(buffer(&(pBuf->front()), szBuffer),
[=](const boost::system::error_code &error, size_t byTrans) {
this->doneRead(callBack, pBuf, error, byTrans); });
这可以正常编译并运行正如预期的那样,“doneRead”从“async_read_some”回调,
因此我在自己的代码中也有类似的回调:
client->asyncRead([=](string msg){this->newMsg(msg); });
这仅需要一个字符串,asyncReads原型如下
void ClientConnection::asyncRead(void(*callBack)(string))
但我收到此编译错误:
Server.cpp:在成员函数“void”中 服务器::clientAccepted(std::shared_ptr, const boost::system::error_code&)': Server.cpp:31:3: 错误:没有匹配 调用函数 'ClientConnection::asyncRead(服务器::clientAccepted(std::shared_ptr, 常量 boost::system::error_code&)::)' Server.cpp:31:3:注意:候选者是: ClientConnection.h:16:9:注意:无效 ClientConnection::asyncRead(无效 (*)(std::字符串)) ClientConnection.h:16:9:注意:否 参数 1 的已知转换自 '服务器::客户端接受(std::shared_ptr, 常量 boost::system::error_code&)::' 到 'void (*)(std::string)'
这个问题如何解决?
I am currently writing a program in C++0x which I am fairly new to.
I am setting up callbacks between objects and using lambda to match the types (like boost::bind()
does in ways)
If I call a function in the asio library like:
socket_.async_read_some(buffer(&(pBuf->front()), szBuffer),
[=](const boost::system::error_code &error, size_t byTrans) {
this->doneRead(callBack, pBuf, error, byTrans); });
This compiles fine, and runs as expected, 'doneRead' is called back from 'async_read_some'
so I have a similar call back in my own code:
client->asyncRead([=](string msg){this->newMsg(msg); });
This takes just a string, and asyncReads prototype is as follows
void ClientConnection::asyncRead(void(*callBack)(string))
But I get this compile error:
Server.cpp: In member function ‘void
Server::clientAccepted(std::shared_ptr,
const boost::system::error_code&)’:
Server.cpp:31:3: error: no matching
function for call to
‘ClientConnection::asyncRead(Server::clientAccepted(std::shared_ptr,
const
boost::system::error_code&)::)’
Server.cpp:31:3: note: candidate is:
ClientConnection.h:16:9: note: void
ClientConnection::asyncRead(void
(*)(std::string))
ClientConnection.h:16:9: note: no
known conversion for argument 1 from
‘Server::clientAccepted(std::shared_ptr,
const
boost::system::error_code&)::’
to ‘void (*)(std::string)’
How can this issue be resolved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 lambda 隐式捕获
this
。捕获事物的 lambda 无法转换为原始函数指针。因此,您需要编写
asyncRead
,以便它直接接受lambda函数对象,而不是让它转换为函数指针。或者,如果您不想将其编写为模板,则可以使用多态函数对象包装器
我还会考虑更改回调的接口,以便它通过 const 引用接受字符串(除非所有回调实现本质上都希望在内部修改或保存/移动传递的字符串,这在您的情况下似乎不太可能)。
Your lambda captures
this
implicitly. A lambda that captures things cannot convert to a raw function pointer.So you need to write
asyncRead
so it accepts the lambda function object directly, instead of letting it convert to a function pointerAlternatively, if you don't want to write this as a template, you can use a polymorphic function object wrapper
I would also consider changing the callback's interface so it accepts the string by const reference (unless all the callback implementations inherently want to modify or save/move the passed string internally, which seem unlikely in your case).