I often tend to have interfaces which take a boost::function<void()> that is to be called when an event occurs. Quite often I need make two separate function calls as response to one event. In these cases I usually solve the problem using the following technique: void foo(); void bar(); void call_two(const boost::function<void ()>& one, const boost::function<void ()>& two) { one(); two(); } boost::function<void ()> on_event = boost::bind(&call_two, boost::bind(&foo), boost::bind(&bar)); Is there any other way to acheive this, beside having to redeclare and define a function such as call_two? It seems quite easy to define something like: template <typename R1, typename R2> void nullary_compose boost::function<R1 ()>& one, const boost::function<R2 ()>& two); template <typename R1, typename R2, typename R3> void nullary_compose boost::function<R1 ()>& one, const boost::function<R2 ()>& two, const boost::function<R3 ()>& three); ... Is anybody else interested in seeing such templates in boost? Best Regards, Johan Torp -- System Developer TouchTable AB Tel: +46 (0)31 773 68 12 johan.torp@touchtable.se
Johan Torp wrote:
I often tend to have interfaces which take a boost::function<void()> that is to be called when an event occurs. Quite often I need make two separate function calls as response to one event. In these cases I usually solve the problem using the following technique:
void foo(); void bar();
void call_two(const boost::function<void ()>& one, const boost::function<void ()>& two) { one(); two(); }
boost::function<void ()> on_event = boost::bind(&call_two, boost::bind(&foo), boost::bind(&bar));
Why not just use Boost Signals to have a single signal just call your two different slots of foo and bar ? It seems you are unnecessarily trying to re-invent a wheel which has already been re-invented by the signals library.
Johan Torp wrote:
I often tend to have interfaces which take a boost::function<void()> that is to be called when an event occurs. Quite often I need make two separate function calls as response to one event. In these cases I usually solve the problem using the following technique:
void foo(); void bar();
void call_two(const boost::function<void ()>& one, const boost::function<void ()>& two) { one(); two(); }
boost::function<void ()> on_event = boost::bind(&call_two, boost::bind(&foo), boost::bind(&bar));
#include <boost/lambda/bind.hpp> boost::function<void ()> on_event = (boost::lambda::bind(&foo), boost::lambda::bind(&bar)); // Johan (Nilsson)
participants (3)
-
Edward Diener
-
Johan Nilsson
-
Johan Torp