ROS has a bunch of subcribe functions that are available from ros::NodeHandle
. Almost all of them are overloaded to take free function, or class member functions as arguments. There are 2 that make it possible to use stateful functor objects:
template<class M>
Subscriber subscribe(const std::string& topic, uint32_t queue_size, const boost::function<void (const boost::shared_ptr<M const>&)>& callback,
const VoidConstPtr& tracked_object = VoidConstPtr(), const TransportHints& transport_hints = TransportHints())
template<class M, class C>
Subscriber subscribe(const std::string& topic, uint32_t queue_size, const boost::function<void (C)>& callback,
const VoidConstPtr& tracked_object = VoidConstPtr(), const TransportHints& transport_hints = TransportHints())
#1 takes a functor that has the signature: void( const MsgConstSharedPointer& ptr)
. This is really weird, A const&
to a std::shared_ptr<const T>
... why? The shared pointer is pointless (no pun intended) here, a const & message object would do.
#2 takes a functor with a templated signature: void(const Msg&)
. This is much more sensible. But the template signature (template<class M, class C>
) is such that the type M is never used anywhere. So the only way to actually get this function to be looked up is to use it like:
auto my_subscriber = node_handle.subscribe<void, MsgType>(... , [](const MsgType& msg) {...}, ...);
Note the void
Is there a more sensible way to invoke this specific overload (aka without having to fake the template parameter)?