ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Template argument deduction fails

asked 2016-05-03 13:43:53 -0500

sterlingm gravatar image

updated 2016-05-04 09:30:35 -0500

Hi,

I have this call for a Subscriber:

ros::Subscriber sub = handle.subscribe<std_msgs::Bool>(topic_str.str(), 10, boost::bind(obIcCb, _1, i));

Why do I need to specify the type for the subscribe call?

If I remove the type specification so that the call is:

ros::Subscriber sub = handle.subscribe(topic_str.str(), 10, boost::bind(obIcCb, _1, i));

Then I get a big error message that says no matching function for call and many instances of template argument deduction/substitution failed like this:

error: no matching function for call to ‘ros::NodeHandle::subscribe(std::basic_stringstream<char>::__string_type, int, boost::_bi::bind_t<void, void (*)(std_msgs::Bool_<std::allocator<void>
>, int), boost::_bi::list2<boost::arg<1>, boost::_bi::value<unsigned char> > >)’
    ros::Subscriber sub = handle.subscribe(topic_str.str(), 10, boost::bind(obIcCb, _1, i));

template argument deduction/substitution failed:
/home/sterlingm/ros_workspace/src/ramp/ramp_planner/src/main_obstacle_twist_all.cpp:357:90: note:   mismatched types ‘void (T::*)(M)’ and ‘boost::_bi::bind_t<void, void (*)(std_msgs::Bool_<std::allocator<void> >, int), boost::_bi::list2<boost::arg<1>, boost::_bi::value<unsigned char> > >’
    ros::Subscriber sub = handle.subscribe(topic_str.str(), 10, boost::bind(obIcCb, _1, i));

When I put the type back in, it compiles. Can someone help explain why I have to specify the type in this call to subscribe? My callback function is not a class member. It's signature looks like this:

void obIcCb(const std_msgs::Bool data, const int index)

Edit: Very minor edit, just wanted to make it clear that the name in the function signature above matches the name I'm using in my subscribe call.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2017-10-03 06:58:43 -0500

130s gravatar image

In Functor Objects section in "roscpp/Overview/Publishers and Subscribers" wiki:

Note: when using functor objects (like boost::bind, for example) you must explicitly specify the message type as a template argument, because the compiler cannot deduce it in this case.

edit flag offensive delete link more

Comments

That must be the issue. Thanks for this follow up.

sterlingm gravatar image sterlingm  ( 2017-10-03 07:04:14 -0500 )edit
1

answered 2016-05-04 09:38:48 -0500

Not sure if this is the problem, but usually my callbacks take a ConstPtr type, e.g.,

void imageCallback (const sensor_msgs::ImageConstPtr& img)

Also the callable can be a functor or function pointer.

Maybe for these reasons type deduction isn't possible in general.

edit flag offensive delete link more

Comments

That was an issue with my code I saw after making this post, but it doesn't fix the template problem.

sterlingm gravatar image sterlingm  ( 2016-05-04 09:56:23 -0500 )edit

My understanding is, that there is no "template problem". You need to state the type of the ros message because it is not deducible by the compiler.

Felix Endres gravatar image Felix Endres  ( 2016-05-09 05:44:22 -0500 )edit

This is not always true though. One can see in the ROS tutorials that the subscribe call compiles without specifying the type: http://wiki.ros.org/ROS/Tutorials/Wri...

sterlingm gravatar image sterlingm  ( 2016-05-09 12:58:18 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-05-03 13:43:53 -0500

Seen: 1,450 times

Last updated: Oct 03 '17