Protobuf版本的std :: String在Oneof中生成的代码差异
我想将Protobuf版本从3.17.0降低到3.12.0,但我会遇到问题。例如:
syntax = "proto3";
message Foo {
optional int32 a = 1;
}
message Test {
oneof value {
string str = 1;
Foo foo = 2;
}
}
对于此秘密,我将其与Protoc(3.17.0)编辑,我在生成的test
类中获得了一个实用方法,以检查str
是否设置在一个Oneof中字段:
// from generated test.pb.h
// string str = 1;
bool has_str() const;
private:
bool _internal_has_str() const;
public:
void clear_str();
// ...
但是,当我使用ProtoC(3.12.0)对其进行编译并设置可选标志时:
./protoc test.proto --cpp_out=./ --experimental_allow_proto3_optional
我得到了生成的代码:
// string str = 1;
private:
bool _internal_has_str() const;
public:
void clear_str();
因此,has_str
不再存在。有人知道为什么吗?谢谢!
PS: has_foo
将始终存在。
I want to degrade my protobuf version from 3.17.0 to 3.12.0, But I get a problem. For example:
syntax = "proto3";
message Foo {
optional int32 a = 1;
}
message Test {
oneof value {
string str = 1;
Foo foo = 2;
}
}
For this protofile, I compile it with protoc(3.17.0), I get a utility method in generated Test
class to check if str
is set in the oneof field:
// from generated test.pb.h
// string str = 1;
bool has_str() const;
private:
bool _internal_has_str() const;
public:
void clear_str();
// ...
But when I use protoc(3.12.0) to compile it and set the optional flag:
./protoc test.proto --cpp_out=./ --experimental_allow_proto3_optional
I got the generated code:
// string str = 1;
private:
bool _internal_has_str() const;
public:
void clear_str();
So ,has_str
doesn't exist anymore. Does anybody know why? Thanks!
PS:has_foo
will always exist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
这与包括非实验proto3可选支持的相同版本是。
在该版本之前,一个人可以使用生成的
_case ()
函数检测设置了哪个单字段。The has accessors in C++ for oneof fields were added in 3.15:
This is coincidentally the same version that includes non-experimental proto3 optional support:
Prior to that version, one can use the generated
_case()
function to detect which oneof field is set.