使用匿名或 lambda 函数连接到 Boost Signals2 信号

发布于 2024-10-05 08:09:29 字数 587 浏览 5 评论 0原文

我正在尝试执行以下操作,以便从升压信号接收字符串并将其发布到显示器上。以下语法不正确。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舞袖。长 2024-10-12 08:09:36

AfxMessageBox是__stdcall函数,要支持此类函数,您需要在#include之前定义BOOST_BIND_ENABLE_STDCALL:

#define  BOOST_BIND_ENABLE_STDCALL
#include <boost\bind.hpp>

int _tmain(int argc, _TCHAR* argv[])
  {
  boost::bind<int, LPCTSTR>(
    &AfxMessageBox, L"", 0, 0);
  }

AfxMessageBox is __stdcall function, to support such functions you need to define BOOST_BIND_ENABLE_STDCALL before #include:

#define  BOOST_BIND_ENABLE_STDCALL
#include <boost\bind.hpp>

int _tmain(int argc, _TCHAR* argv[])
  {
  boost::bind<int, LPCTSTR>(
    &AfxMessageBox, L"", 0, 0);
  }
诠释孤独 2024-10-12 08:09:35

AfxMessageBox 有几个重载和默认参数,这使得您的构造上面不明确。编写一个仅采用一个 LPCSTR 的小函数,该函数转发到 AfxMessageBox,并将其绑定到 signal<>。

编辑:
由于有些人似乎不喜欢我上面提供的内容(为什么在没有评论的情况下投票?)这里有一些澄清我上面写的代码:

int MyMessageBox(LPCSTR msg)
{
    return AfxMessageBox(msg);
}

UserMessageEvent.connect(boost::bind(MyMessageBox, _1));

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:

int MyMessageBox(LPCSTR msg)
{
    return AfxMessageBox(msg);
}

UserMessageEvent.connect(boost::bind(MyMessageBox, _1));
停顿的约定 2024-10-12 08:09:33

我不知道 boost::bind 对于默认参数的行为如何。

无论如何,这是 lambda 的语法:

UserMessageEvent.connect( [](const char* message)
{
   // maybe need here a conversion to LPCWSTR ?
   AfxMessageBox(message);
});

I don't know how boost::bind behave in regard to default parameters.

Anyway, here is the syntax with lambda :

UserMessageEvent.connect( [](const char* message)
{
   // maybe need here a conversion to LPCWSTR ?
   AfxMessageBox(message);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文