How to use topic message type as an argument of a function?
In the subscriber callback function, I would like to use topic message type as an argument so that I can use for any topic:
void callback(message_type &msg)
{
....
}
....
ros::Subscriber sub = nh.subscribe(topic_name, 1000, callback);
Any suggestion?
Asked by Kevin1719 on 2021-11-21 07:55:56 UTC
Answers
I'm 99% certain that this can not be done. In c++, ros1 messages do not inherit from a common base class, so one message's type is unrelated to all other message types (in a class hierarchy sense.) By definition, a topic can only have one message type. To help avoid bugs, the ros developers decided to make you declare exactly what message type you think is arriving on the topic.
Asked by Mike Scheutzow on 2021-11-21 10:17:24 UTC
Comments
It can be done, but I've never tried it. It can also be done with templates, I would guess, but perhaps if the OP described in more detail what they wanted to do...
Asked by KenYN on 2021-11-22 03:03:31 UTC
@KenYN Sometimes it's best to recognize that the cure is worse than the disease. But thanks for the link - that is an approach I haven't seen before.
Asked by Mike Scheutzow on 2021-11-22 14:57:44 UTC
Comments