Ask Your Question
1

Passing ros message field argument as reference

asked 2018-10-26 08:51:45 -0500

Delb gravatar image

updated 2018-10-29 02:23:19 -0500

Hi all,

Here is a little code illustrating my issue :

void test2(bool &test)
{    
    return;
}
void test(std_msgs::Bool &bool_test)
{
    //TEST 1
    bool a_bool;
    test2(a_bool); //Ok

    //TEST 2
    a_bool = bool_test.data;
    test2(a_bool); //Ok

   //TEST 3
    bool_test.data = a_bool;
    test2(bool_test.data); //Not okay
    return;
}

I'm not able to compile something like this and I don't know how I can use a function taking as argument a reference of the same type of one field of a ros message. The error is the following :

error: invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘bool’
     test2(odom_test.data);

I'm a little bit confused about this rvalue in the error. I've looked into it and the definition is :

rvalue is an expression that can only appear on the right hand side of an assignment

Why does bool_test is considered as rvalue ? If it was true I wouldn't be able to write bool_test.data = a_bool; in TEST 3.

I know that I could do like TEST 2 and create a variable for each field of my message but I would like to understand why the TEST 3 isn't compiling. Is it even possible ?

NB : I used std_msgs::Bool and a function with bool argument just as an example, it happens with other type messages too.

edit retag flag offensive close merge delete

Comments

I've deleted my answer as I misunderstood the issue. Seems like it might not be possible with std_msgs/Bool, but I'm not sure why. The issue can be made more succinctly:

std_msgs::Bool my_bool;
bool & ref_my_bool = my_bool.data;
jacobperron gravatar image jacobperron  ( 2018-10-26 13:30:22 -0500 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2018-10-26 13:37:48 -0500

jacobperron gravatar image

updated 2018-10-29 11:54:42 -0500

After some digging, it would appear that the underlying type of std_msgs::Bool.data is uint8_t (source).

This is why something like the following fails to compile:

std_msgs::Bool my_bool;
bool & ref_my_bool = my_bool.data;

Perhaps a workaround is to use uint8_t type explicitly:

std_msgs::Bool my_bool;
uint8_t & ref_my_bool = my_bool.data;

Or passing by reference to a function:

#include <stdint.h>
#include <std_msgs/Bool.h>

void data_by_ref(uint8_t &test)
{
    return;
}
void std_bool_by_ref(std_msgs::Bool &bool_test)
{
    data_by_ref(bool_test.data);
}

int main(void)
{
  std_msgs::Bool my_bool = std_msgs::Bool();
  std_bool_by_ref(my_bool);
  return 0;
}
edit flag offensive delete link more

Comments

Thanks for the reply, unfortunately it's giving the same error, so yes uint8_t is the right type for bool but I would like to know if it's possible without using another variable and directly have a function correctly defined to accept a reference to bool_test.data as argument.

Delb gravatar image Delb  ( 2018-10-27 10:16:09 -0500 )edit

I've updated my answer to contain a complete example of what I think you are asking. As long as the callback signature for the data member has type uint8_t, you shouldn't need another variable as described. Changing your example fails to compile because of "TEST 1".

jacobperron gravatar image jacobperron  ( 2018-10-29 11:57:22 -0500 )edit

Thank you that's exactly what I've been looking for ! (Sorry I just realised your previous answer worked too but I misread that you changed the argument in the function from bool to uint8_t which was the main issue)

Delb gravatar image Delb  ( 2018-10-30 02:13:44 -0500 )edit
0

answered 2019-01-31 21:08:37 -0500

Actually, considering the error nature: error: invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘bool’, the figure below illustrates that:

linker error

The correct way to solve this bug is passing this rvalue (bool_test.data) by const to reference, once the compiler denies the reference passing of an rvalue (temporary and literal types) without being const because it does not create a non-const reference to rvalues preventing operations like "3 = 5" and other stuff. The right way to do that is:

#include <ros/ros.h>
#include <std_msgs/Bool.h>
#include <stdint.h>

void test2(const uint8_t &test)
{    
    return;
}
void test(const std_msgs::Bool &bool_test)
{
    test2(bool_test.data); //Not okay before, But OKAY now hahahaha
    return;
}

int main(int argc, char** argv)
{
    std_msgs::Bool bool_obj;
    test(bool_obj);

    return 0;
}

Moreover, remember, always try to use pass by const-to-reference, once errors like that are avoided and, consequently, you avoid other variables values changing and helps the compiler to optimize the code execution.

I hope to help you! If you have some question do not hesitate to ask me.

edit flag offensive delete link more

Comments

Please don't use an image to display text. The text in images is not searchable and people cannot copy and paste the text from the image either. Please update your answer with a copy and paste of the text instead of a screenshot.

jayess gravatar image jayess  ( 2019-01-31 21:14:48 -0500 )edit

Thanks for your advice. I'm going to keep this in mind for the next answers. However, the message of the image in question was already put at the question. I put the image just to illustrate that the same error happened during my compilation, i.e., to emphasize my point of view.

matheusns gravatar image matheusns  ( 2019-01-31 21:40:42 -0500 )edit

However, if you think that the image can affect the good practices, I can remove it peacefully.

matheusns gravatar image matheusns  ( 2019-01-31 21:42:30 -0500 )edit
1

I actually asked the question to avoid using const references but thanks, maybe your answer will help somebody. Also as @jayess said you should remove or simply copy/paste the content of your image because it's not even readable like that.

Delb gravatar image Delb  ( 2019-02-01 02:19:08 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

3 followers

Stats

Asked: 2018-10-26 08:51:45 -0500

Seen: 495 times

Last updated: Jan 31 '19