Does protocol buffer support move?

I checked the move constructor spec and message constructor and did not find it.

If not, does anyone know of a plan to add it?

I use the proto3 syntax, write a library, and consider between returning through the value vs unique_ptr.

+6
source share
3 answers

Starting with version 2.6.1, the C ++ protobuf compiler generates only copy constructors and copy assignment operators. But if your compiler supports optimizing the return value (and the conditions for this are met), the copy constructor will not be called in any case.

You can add some print statements to the generated copy constructor code of your messages to see if they are actually called or not. You can also do this by writing the protoc plugin so that it persists between protoc calls.

+4
source
  • If you try to use an assignment operator, RVO will perform an 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 the lvalue to the list / sub message, etc. Try using the ConcreteMessage::Swap method. Changed 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; 
+5
source

According to https://github.com/google/protobuf/issues/2791 , this will be supported in Protobuf version 3.4.0.

+5
source

Source: https://habr.com/ru/post/987420/


All Articles