升压::绑定& boost::function 指向重载或模板化成员函数的指针

发布于 2024-08-15 05:58:48 字数 1133 浏览 2 评论 0原文

我有一个回调机制,涉及的类是:

class App
{
    void onEvent(const MyEvent& event);
    void onEvent(const MyOtherEvent& event);

    Connector connect;
}

class Connector
{  
   template <class T> void Subscribe(boost::function <void (const T&)> callback);
}

App::App()
{
    connect.Subscribe<MyEvent>(&App::OnEvent<MyEvent>);
}

首先,这段代码无法编译,它只是一个说明。

模板的使用使我的示例变得复杂,但我保留了它们,因为它影响了我的问题。在我看来,我的订阅需要模板化,因为 Connector 类不知道它处理多少个事件类型。当我尝试创建 a:

boost::function f = &App::OnEvent, 

我尝试将 OnEvent 创建为具有专门化的模板函数,但编译器似乎将我的 OnEvent 函数视为重载而不是模板专门化,否则如果我得到模板专门化不在命名空间错误尝试显式声明它,因为

template <> OnEvent(const MyEvent& e) ...

我可以编译以下内容:

boost::function <void (App*, const MyEvent&)> f = &App::OnEvent;
f(this, e); 

编译、运行和工作。

boost::function<void (const MyEvent&)> g = boost::bind(&App::OnEvent, this);

没有。我认为这是因为我没有正确指定重载函数的地址。

现在向泰迪熊解释了这一切 - 我认为我的问题是“如何正确创建指向重载或模板化成员函数的函数指针并将 this 指针绑定到它?”

I have a callback mechanism, the classes involved are:

class App
{
    void onEvent(const MyEvent& event);
    void onEvent(const MyOtherEvent& event);

    Connector connect;
}

class Connector
{  
   template <class T> void Subscribe(boost::function <void (const T&)> callback);
}

App::App()
{
    connect.Subscribe<MyEvent>(&App::OnEvent<MyEvent>);
}

First off this code doesn't compile, it's an illustration.

The use of templates complicates my example, but I left them in because its affecting my problem. It seems certain to me that my subscribe needs to be templated because the Connector class doesn't know how many event types it handles. When I try to create a:

boost::function f = &App::OnEvent, 

I tried creating OnEvent as a template function, with specializations, but it seems that the compiler is treating my OnEvent functions as overloads rather than template specializations, or else I get the template specialization not in namespace error if I try to explicitly declare it as

template <> OnEvent(const MyEvent& e) ...

I can get the following to compile:

boost::function <void (App*, const MyEvent&)> f = &App::OnEvent;
f(this, e); 

That compiles, runs, and works.

boost::function<void (const MyEvent&)> g = boost::bind(&App::OnEvent, this);

does not. I think its because I'm not correctly specifying the address of an overloaded function.

Having now explained all this to the teddy bear - I think that my question is "How do I correctly create a function pointer to an overloaded or templated member function and bind the this pointer to it?"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

流年已逝 2024-08-22 05:58:48

我认为您需要消除重载函数的地址的歧义。您可以通过将函数指针显式转换为具有正确参数的函数指针来完成此操作。

boost::bind( static_cast<void (App::*)( MyEvent& )>(&App::OnEvent) , this, _1);

gamedev.net 上的类似问题+解决方案

I think you need to disambiguate the address of the overloaded function. You can do this by explicitly casting the function pointer to the one with the correct parameters.

boost::bind( static_cast<void (App::*)( MyEvent& )>(&App::OnEvent) , this, _1);

Similar problem + solution on gamedev.net

冷心人i 2024-08-22 05:58:48

你会想要做

boost::function<void (const MyEvent&)> g = boost::bind(&App::OnEvent, this, _1);

你应该能够做 g(event); 我不太

确定你想在这里完成什么,但这应该暂时解决你的问题之一。

You are going to want to do

boost::function<void (const MyEvent&)> g = boost::bind(&App::OnEvent, this, _1);

You should be able to do g(event); with that

I am not quite sure what you are trying to accomplish here, but that should solve one of your problems for now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文