消除对Boost ::的需求,以绑定我的功能
removing the need for boost::bind in my function
这是一个中层问题。我通过存储指向功能的指针来注册我的活动。当前,我将功能存储在boost::function<void (Event*)>
中,该功能应在启动事件时将事件指向事件作为参数。
要注册一个新事件,我要这样做..
Event::Register("click", "Image18", boost::bind(&MyMenu::OnClick, this, _1));
MyMenu::OnClick(DKEvent* event) <-- calls this
我想尽可能简化代码的用户端。如何将boost :: bind方法推入事件类。我想减少注册活动,以下。少是好。
Event::Register("click", "Image18", &MyMenu::OnClick, this);
这可能涉及使用C 模板,因为所引用的类并不总是相同的。有人可以快速看一下我如何实现这一目标。
您可以将 Event::Register
纳入模板函数,以成员函数和实例为参数:
//templated on the class and the return type and args of the member function
template <class T, typename Ret, typename... Args>
void Event::Register (std::string, std::string, Ret (T::*) (Args...), T*);
您甚至不需要在此处提供模板参数,编译器将为您推论。
您需要 variadic模板
template<typename... Args>
void Event::Register(std::string ID, Args... args)
{
auto bind = std::bind(args...);
}
不需要boost::bind
。
您正在限制用户,并强迫他在实例上使用会员指针功能,而如果您允许它,则您将从多个来源中受益可以绑定到包括lambdas和自由功能的std::function
,IMO您当前的表格比您想要实现的表单要好得多。
最好让您的班级用户选择他是否要将实例绑定到成员函数,您会通过提供多个模板过载(例如boost::bind
divedive)来使界面复杂化。
相关文章:
- 完美转发使用结构化绑定声明的变量
- 提升 ASIO 绑定:错误的文件描述符
- 如何使用libpq库和C 绑定无效的语句
- C++中名称查找和名称绑定之间的区别
- 迭代从 SWIG 绑定返回的 python 中的 vector<pair<int,int>>
- 如何使用 Poco::Net::HTTPSClientSession 绑定特定的源 IP 地址
- C++17:是编译器为(静态存储持续时间)const引用绑定创建的可修改的临时对象(和存储)
- 为每个对象绑定不同的纹理
- 结构化绑定中的const引用扩展了分解对象的寿命
- xamarin-ios绑定上的本地链接错误
- 没有重新绑定模板的类型定义模板.用作模板类参数的模板
- 为什么从绑定返回的对象忽略额外的参数
- 消除对Boost ::的需求,以绑定我的功能
- 如何定义一个仅绑定 xvalue 或 prvalue 而不绑定两者的函数
- 创建具有提升绑定/函数的菜单处理程序
- 缺少对指向成员函数的绑定指针的调用
- vtable(动态绑定)的C++思想中的多态性
- 如何在控制台中获得我的进程绑定到的行字符数
- 我如何使用tr1函数和绑定函数的变化参数
- 我该如何使我的分配器可重新绑定?我能做到这一点,同时保持其字段的私有