Protobuf版本的std :: String在Oneof中生成的代码差异

发布于 2025-02-12 00:50:19 字数 869 浏览 0 评论 0原文

我想将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夢归不見 2025-02-19 00:50:19

现在,原始田地具有“有”用于检查其存在的方法
C ++。

这与包括非实验proto3可选支持的相同版本是

默认情况下启用了proto3的可选字段,不再需要
-experimention_allow_proto3_optional标志。

在该版本之前,一个人可以使用生成的_case ()函数检测设置了哪个单字段。

The has accessors in C++ for oneof fields were added in 3.15:

Now Proto3 Oneof fields have "has" methods for checking their presence in
C++.

This is coincidentally the same version that includes non-experimental proto3 optional support:

Optional fields for proto3 are enabled by default, and no longer require
the --experimental_allow_proto3_optional flag.

Prior to that version, one can use the generated _case() function to detect which oneof field is set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文