【问题标题】:In boost::signals, what is the difference between 'slot_type' and 'slot_function_type'?在 boost::signals 中,“slot_type”和“slot_function_type”有什么区别?
【发布时间】:2012-08-15 05:42:18
【问题描述】:
在 boost::signals 库的 this tutorial 末尾,在“传递槽”标题下,一个名为“slot_type”的类型用于传递要连接到信号的所需槽函数。但是,在next example 中,他们使用一种名为“slot_function_type”的类型显然做了同样的事情。
我一直无法弄清楚这两件事之间的区别。
什么时候应该使用'slot_type',什么时候应该使用'slot_function_type'?
【问题讨论】:
标签:
c++
boost
boost-signals
【解决方案1】:
slot_type 是boost::slot<slot_function_type>。
对于信号boost::signal<R(T1, T2, ... TN)>,slot_function_type 是类型擦除的函数对象类boost::functionN<R, T1, T2, ... TN>,等价于std::function<R(T1, T2, ... TN)>,因此它可以保存任何支持信号调用签名的对象(例如函数指针、可调用对象、 boost::bind等)。
slot_type 包含并具有来自slot_function_type 的隐式构造函数,但它还包含通过boost::trackable 进行自动连接管理的机制(参见教程中的Automatic connection management (Intermediate))。
您想使用slot_type,除非您有特定的理由使用slot_function_type,因为这将确保如果您需要使用自动连接管理,它就可以正常工作。 slot_type 有一个模板化的隐式构造函数,所以可以在任何地方使用 slot_function_type 来传递给 boost::signal::connect()。