是否可以在google protobuf中为类型(枚举或消息)定义别名?

vro*_*ach 5 c++ enums protocol-buffers

我的proto文件中有两个枚举,它们定义了几乎相同的值.

是否可以删除其中一个并留下别名以保持所有代码正常工作?

例:

enum A {
   a = 0;
   b = 1;
}
enum B {
   a = 0;
   b = 1;
}
Run Code Online (Sandbox Code Playgroud)

我希望在c ++中有类似typedef的东西:

enum A {
   a = 0;
   b = 1;
}

typedef A B;
Run Code Online (Sandbox Code Playgroud)

我没有在文档中找到这个.有没有解决方法?

Fre*_*oul 6

这是一个老问题,但如果有些人仍然感兴趣,现在可以使用 protobuf 在 enum 上创建别名

enum EnumAllowingAlias {
  option allow_alias = true;
  UNKNOWN = 0;
  STARTED = 1;
  RUNNING = 1;
}
enum EnumNotAllowingAlias {
  UNKNOWN = 0;
  STARTED = 1;
  // RUNNING = 1;  // Uncommenting this line will cause a compile error inside Google and a warning message outside.
}
Run Code Online (Sandbox Code Playgroud)

正如官方文档中所解释的那样。您只需要通过添加来启用别名选项option allow_alias = true;


Gio*_*hal 5

从protobuf版本3开始,这是不可能的.