Search code examples
c++boostboost-signals

In boost::signals, what is the difference between 'slot_type' and 'slot_function_type'?


Towards the end of this tutorial for the boost::signals library, under the heading "Passing slots" a type called 'slot_type' is used to pass the desired slot function to be connected to the signal. But then, in the next example, they do apparently the same thing using a type called 'slot_function_type'.

I haven't been able to work out what the difference is between these two things.

When should I use 'slot_type' and when should I use 'slot_function_type'?


Solution

  • slot_type is boost::slot<slot_function_type>.

    For a signal boost::signal<R(T1, T2, ... TN)>, slot_function_type is the type-erased function object class boost::functionN<R, T1, T2, ... TN>, equivalent to std::function<R(T1, T2, ... TN)>, so it can hold any object supporting the signal's call signature (e.g. function pointers, callable objects, boost::bind, etc.).

    slot_type contains and has an implicit constructor from slot_function_type, but it also contains the machinery for automatic connection management through boost::trackable (see Automatic connection management (Intermediate) in the tutorial).

    You want to use slot_type unless you have some specific reason to use slot_function_type, as this will ensure that if you ever need to use automatic connection management it will just work. slot_type has a templated implicit constructor, so it's OK to use anywhere slot_function_type would work for passing to boost::signal::connect().