在枚举中添加枚举符是否会破坏ABI? [英] Does adding enumerators into enum break ABI?

查看:91
本文介绍了在枚举中添加枚举符是否会破坏ABI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

特别是,我在库界面中获得了以下代码:

In particular, i got following code in library interface:

typedef enum
{
    state1,
    state2,
    state3,
    state4,
    state5,
    state_error = -1,
} State;

我严格禁止破坏ABI。但是,我想添加state6和state7。会破坏ABI吗?

I strictly forbidden to break ABI. However, I want to add state6 and state7. Will it break ABI?

我发现此处一些提示,但我有点怀疑是否是我的情况?

I found here some tip, but i somewhat doubt if it`s my case?


您可以...

You can...


  • 将新的枚举数追加到现有枚举上。

示例:如果导致编译器为枚举选择更大的基础类型,则更改二进制不兼容。不幸的是,编译器有一些选择基础类型的余地,因此从API设计的角度来看,建议添加一个Max ....枚举器,该枚举器具有显着的大值(= 255,= 1<< 15等)来创建数值枚举器值的区间,该区间一定可以适合所选的基础类型,无论可能是什么。

Exeption: if that leads to the compiler choosing a larger underlying type for the enum,that makes the change binary-incompatible. Unfortunately, compilers have some leeway to choose the underlying type, so from an API-design perspective it's recommended to add a Max.... enumerator with an explicit large value (=255, =1<<15, etc) to create an interval of numeric enumerator values that is guaranteed to fit into the chosen underlying type, whatever that may be.


推荐答案

您的问题是一个很好的例子,为什么长期保持ABI兼容性是一项艰巨的任务。问题的核心在于兼容性不仅取决于给定的类型,还取决于它在函数/方法原型或复杂类型(例如结构,联合等)中的使用方式。

Your question is a nice example why long-term maintaining of ABI compatibility is a difficult task. The core of the problem here is that the compatibility depends not just on the given type, but also on how it is used in function/method prototypes or complex types (e.g. structures, unions etc.).

(1)如果枚举在任何地方用作库的输出(例如,返回值或函数填充了调用方(又称为输出参数)提供的某些地址),则更改将中断ABI。将该枚举看作是一个合同,上面写着应用程序看不到列出的值之外的其他值。添加新的枚举成员将破坏此合同,因为旧的应用程序现在可以看到它们从未使用过的值。

(1) If the enumeration is used anywhere as an output from the library (e.g. return value or function filling some address provided by caller a.k.a output parameter), the change would break the ABI. Consider the enumeration to be a contract saying "the application never sees values other then those listed". Adding new enum member would break this contract because old applications could now see values they never counted with.

(2)如果严格将枚举用作库的输入(例如,作为仅改变功能/库行为的功能的参数),然后保持兼容性:您以永远不会伤害客户(即调用应用程序)的方式更改合同。旧的应用程序永远不会使用新的值,而会获得旧的行为,新的应用程序只会获得更多的选择。

(2) If the enumeration is used strictly as an input into the library (e.g. as a parameter of a function which just changes of behavior the function/library), then it keeps the compatibility: You changed the contract in a way which can never hurt the customer i.e. the calling application. Old applications shall never use new value and will get old behavior, new applications just get more options.

这篇关于在枚举中添加枚举符是否会破坏ABI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆