class RadioContainer
{
public:
void handle_ack_message(byte&& msg);
...
};
errors:
/tmp/181747193/build/sketch/Radio.cpp: In member function 'void RadioContainer::radio_transmit_message(byte)':
/tmp/181747193/build/sketch/Radio.cpp:76:27: error: cannot bind 'byte {aka unsigned char}' lvalue to 'byte&& {aka unsigned char&&}'
handle_ack_message(msg);
^
In file included from /tmp/181747193/build/sketch/Radio.cpp:3:0:
/tmp/181747193/build/sketch/Radio.h:60:10: note: initializing argument 1 of 'void RadioContainer::handle_ack_message(byte&&)'
void handle_ack_message(byte&& msg);
^
exit status 1
My grasp of this is tenuous at best, but I'll take a shot. Inside your function 'void RadioContainer::radio_transmit_message()', msg is a local variable -- therefore, it's a 'lvalue'. So, when you call 'handle_ack_message()' from this function, you're trying to pass an 'lvalue' to a function that only accepts an 'rvalue'.
gfvalvo:
My grasp of this is tenuous at best, but I'll take a shot. Inside your function 'void RadioContainer::radio_transmit_message()', msg is a local variable -- therefore, it's a 'lvalue'. So, when you call 'handle_ack_message()' from this function, you're trying to pass an 'lvalue' to a function that only accepts an 'rvalue'.
That's my best guess anyway.
That's not it. You can pass lvalues to functions taking rvalues as arguments (tested using a C++ editor).
ningaman151:
How would you go about doing it for expensive operations?
If you're only going to use the object in the functions you're calling, pass it by const reference.
If you're going to use it to construct the object you're going to return, or if you are going to give it away to somewhere else, use std::move (or std::forward if you're using templates and universal references).
PieterP:
If you're only going to use the object in the functions you're calling, pass it by const reference.
If you're going to use it to construct the object you're going to return, or if you are going to give it away to somewhere else, use std::move (or std::forward if you're using templates and universal references).
I thought the standard library is unavailable for Arduino (AVR in general)
ningaman151:
I thought the standard library is unavailable for Arduino (AVR in general)
Correct.
But this is not an issue, I maintain a library that supports parts of the STL. Things like std::move are nothing special, and there's no reason not to use them on a microcontroller. (Well, using std::move kind of implies dynamic memory usage, but still, std::move itself is just a cast to an rvalue reference.)
Just #include <AH/STL/utility> to use std::move and std::forward etc.
If the STL is supported on the platform you're using (i.e. everything but AVR), it uses the toolchain's version, otherwise, it uses the fallback version provided by the library.