简体   繁体   中英

Protobuf Message Constructor

Far as I understand there when compiling a *.proto file the generated class does not have a constructor nor a copy/move constructor. For example if I have

message Float3Vector{
    optional float x = 1;
    optional float y = 2;
    optional float z = 3;
}

I can't call Float3Vector(my_x, my_y, my_z) . This is clear also from the documentation .

So far so good. There are other ways provided. My question is, is there a particular reason for doing this? I mean, there is a technical reason (ie dictated by serialising, generating code, etc) that makes this impossible?

There seems to be an undocumented way of doing this. I find it rather annoying how hard constructing protobuf Messages is.

Float3Vector(x=my_x, y=my_y, z=my_z)

This means you can also do things like:

pb_args = {'x':1, 'y':2, 'z':3}
Float3Vector(**pb_args)

see this for more info.

Copy was disabled on purpose. For big protobufs copy is expensive and to prevent accidental copy it was chosen to make CopyFrom() explicit.

Move was not needed and was neglected when C++ 11 arrived. The next protobuf release likely have move included.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM