0

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)?

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • " A const& to a std::shared_ptr ... why?" -- why not? If objects are normally managed by shared pointers it makes perfect sense to use shared pointers directly, as parameters, when passing these objects between functions. – Sam Varshavchik Sep 11 '22 at 23:32
  • 3
    @SamVarshavchik I don't think I agree with you here. It makes more sense to pass the stored`T` as either a pointer or a reference. Passing a reference to a `std::shared_ptr` is odd to me. Passing by value would make sense if you're concerned about life time. – WBuck Sep 12 '22 at 00:34
  • @SamVarshavchik A const& to a std::shared_ptr is a very awkward way to pass a reference to an immutable object. I have seen this no-where except ROS, but that is just an opinion. I would like to uise the `const &` invocation. – Fantastic Mr Fox Sep 12 '22 at 23:44
  • @JaMiT there is a link here: http://docs.ros.org/en/kinetic/api/roscpp/html/classros_1_1NodeHandle.html#a317fe4c05919e0bf3fb5162ccb2f7c28 But you have the 2 i have mentionded which take `boost::function` objects. – Fantastic Mr Fox Sep 12 '22 at 23:50
  • (As for the actual question, I know of an unusual case where a const shared pointer reference makes sense, but I do not know ROS well enough to know if that is applicable here. Doubtful, but not certain.) – JaMiT Sep 13 '22 at 02:58
  • @JaMiT Sorry, when i see free function, i mean that the member function on the subscriber takes a function pointer which only supports a free function. – Fantastic Mr Fox Oct 18 '22 at 23:04
  • @JaMiT There are functor objects. https://stackoverflow.com/questions/356950/what-are-c-functors-and-their-uses for eg. `lambda` functions are functor objects. – Fantastic Mr Fox Oct 19 '22 at 01:30
  • @JaMiT does the edit make sense? – Fantastic Mr Fox Oct 19 '22 at 04:46
  • @FantasticMrFox Yes, better. I would replace *"free function, or class member functions"* with just *"functions"* though. It's a simpler phrasing, and the distinction between "free" and "class member" is not important in this context. On the other hand, if the current version was the original phrasing, I would not have brought it up in the comments. *(I've deleted my half of this exchange from the comments. Feel free to delete your half if you want to tidy up.)* – JaMiT Oct 19 '22 at 23:42
  • @JaMiT I am thinking about deleting the question. It is pretty specific. I am pretty sure there is no better way without editing ros internally. So this question is not that useful. – Fantastic Mr Fox Oct 19 '22 at 23:50

0 Answers0