Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Protocol Buffers support move constructor

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.

like image 223
Paweł Szczur Avatar asked May 18 '15 13:05

Paweł Szczur


2 Answers

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

like image 191
Daniel Schepler Avatar answered Sep 22 '22 05:09

Daniel Schepler


  1. 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();
    
  2. 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;
    
like image 29
Alexander Chen Avatar answered Sep 25 '22 05:09

Alexander Chen