升压::绑定& boost::function 指向重载或模板化成员函数的指针
我有一个回调机制,涉及的类是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要消除重载函数的地址的歧义。您可以通过将函数指针显式转换为具有正确参数的函数指针来完成此操作。
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.
Similar problem + solution on gamedev.net
你会想要做
你应该能够做 g(event); 我不太
确定你想在这里完成什么,但这应该暂时解决你的问题之一。
You are going to want to do
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.