英文:
Any reason to use standard value-wrapper types from google.protobuf?
问题
After the addition of the optional
keyword to protobuf 3, which allows explicit presence tracking of scalar fields, are there any reasons to use value-wrapper types from google.protobuf, like BoolValue or DoubleValue etc? Is there any other usage for those except presence tracking?
英文:
After the addition of optional
keyword to protobuf 3, which allows explicit presence tracking of scalar fields, are there any reasons to use value-wrapper types from google.protobuf, like BoolValue or DoubleValue etc? Is there any other usage for those except presence tracking?
答案1
得分: 1
当您需要将原始数据类型打包到 google::protobuf::Any
中时,您必须使用这些包装器。Any
只能与消息类型一起使用,而不能与原始数据类型一起使用。
// C++ 代码:
DoubleValue b;
b.set_value(2.3);
Any any;
any.PackFrom(b); // 您无法从 double 进行打包
DoubleValue bb;
any.UnpackTo(&bb); // 您无法解包为 double
cout << bb.value() << endl;
英文:
When you need to pack primitive types into google::protobuf::Any
, you have to use these wrappers. Any
can only work with message type, not primitive type.
// C++ code:
DoubleValue b;
b.set_value(2.3);
Any any;
any.PackFrom(b); // You cannot pack from a double
DoubleValue bb;
any.UnpackTo(&bb); // you cannot unpack to a double
cout << bb.value() << endl;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论