使用匿名或 lambda 函数连接到 Boost Signals2 信号
我正在尝试执行以下操作,以便从升压信号接收字符串并将其发布到显示器上。以下语法不正确。
signal<void (const char*)> UserMessageEvent;
// connect anonymous boost function to display message box on user message event
UserMessageEvent.connect(boost::bind(AfxMessageBox, _1));
如果这是 C#,我会执行以下操作,让我相信我想使用 lambda 函数在信号的调用类型和 AfxMessageBox 参数的类型之间进行转换。但是我不清楚如何做到这一点。
UserMessageEvent += (c) => MessageBox.Show((some const char to LPCSTR conversion)c);
有什么建议吗?
编辑: msvc10 给出的错误是 error C2914: 'boost::bind' : 无法推断模板参数,因为函数参数不明确
I am trying to do the following in order to receive a string from a boost signal and post it to the display. The following syntax is incorrect.
signal<void (const char*)> UserMessageEvent;
// connect anonymous boost function to display message box on user message event
UserMessageEvent.connect(boost::bind(AfxMessageBox, _1));
If this were C# I would do the following, leading me to believe I want to use a lambda function to convert between the calling type of the signal and the type of the AfxMessageBox arguments. However it is not clear to me how to do this.
UserMessageEvent += (c) => MessageBox.Show((some const char to LPCSTR conversion)c);
Any suggestions?
Edit:
The error given by msvc10 is error C2914: 'boost::bind' : cannot deduce template argument as function argument is ambiguous
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AfxMessageBox是__stdcall函数,要支持此类函数,您需要在#include之前定义BOOST_BIND_ENABLE_STDCALL:
AfxMessageBox is __stdcall function, to support such functions you need to define BOOST_BIND_ENABLE_STDCALL before #include:
AfxMessageBox 有几个重载和默认参数,这使得您的构造上面不明确。编写一个仅采用一个 LPCSTR 的小函数,该函数转发到 AfxMessageBox,并将其绑定到 signal<>。
编辑:
由于有些人似乎不喜欢我上面提供的内容(为什么在没有评论的情况下投票?)这里有一些澄清我上面写的代码:
AfxMessageBox has several overloads and default parameters, which makes your construct above ambigious. Write a small function taking exactly one LPCSTR, which forwards to AfxMessageBox, and bind that to the signal<>.
EDIT:
As some people seem not to like what I provided above (why downvote without a comment?) here some clarifying code for what I wrote above:
我不知道 boost::bind 对于默认参数的行为如何。
无论如何,这是 lambda 的语法:
I don't know how boost::bind behave in regard to default parameters.
Anyway, here is the syntax with lambda :