Search code examples
c++11protocol-buffersstdmove

Assign RepeatedPtrField to repeated field in a protobuf message


I have a RepeatedPtrField<M::Table> and a protobuf message M as:

message M {
  message Table {
    optional string guid = 1;
    optional int64 schema_version = 2;
    optional int64 data_version = 3;
    repeated Column column = 4;
  }
  repeated Table table = 1;
}

How to I create a instance of M having the contents of RepeatedPtrField. I can write a for loop to copy data explicitly, but I am currently looking for something more concise, preferably using std::move() like optimization.


Solution

  • If you're using a new version of Protobuf, like Protobuf 3.6.0, RepeatedPtrField defines move constructor, and you can call std::move to achieve your goal.

    If you're using an old version, you have to call Swap to do the work, as you mentioned in the comment.