使用Google的protobuf标准值包装类型有何理由?

huangapple go评论46阅读模式
英文:

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(&amp;bb);  // you cannot unpack to a double

cout &lt;&lt; bb.value() &lt;&lt; endl;

huangapple
  • 本文由 发表于 2023年6月4日 23:51:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定