I've checked the move constructor spec and a Message constructor source and didn't find one.
If there's not, does anyone know about a plan to add it?
I'm using proto3
syntax, writing a library and considering between return through value vs unique_ptr.
According to https://github.com/google/protobuf/issues/2791 this will be supported in Protobuf version 3.4.0.
If you try to use a assignment operator, RVO will do optimization to prevent an extra copy.
// RVO will bring the return value to a without using copy constructor.
SomeMessage a = SomeFooWithMessageReturned();
If you want to use std::move
to move a lvalue into a list/sub message, etc. Try to use ConcreteMessage::Swap
method. The swapped item will be useless.
// Non-copy usage.
somemessage.add_somerepeated_message()->Swap(&a);
somemessage.mutable_somesinglar_message()->Swap(&a);
// With message copying
somemessage.add_somerepeated_message()->CopyFrom(a);
*somemessage.mutable_somesinglar_message() = a;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With