C++枚举和编译器依赖

发布于 2024-11-17 11:35:11 字数 341 浏览 3 评论 0原文

我目前有一个带有枚举的代码,其中设置了一个值,其余值由编译器使用前一个值+1来设置,或者我希望如此。

此功能是否依赖于枚举类型编译器,下面通过一个示例来阐明。

enum FUNC_ERROR_CODE    
{
    FUNC_SUCCESS,
    FUNC_ERROR_1 = 24,
    FUNC_ERROR_2,
    FUNC_ERROR_3
}

可以安全地假设 FUNC_ERROR_2 的值为 25,FUNC_ERROR_3 的值为 26,无论使用什么编译器。

我对此进行编码,以便函数可以返回整数值,0 始终表示成功,任何其他值都表示失败。

I currently have code with an enum where one value is set and the rest are left to be set by the compiler using the previous value +1, or so I hope.

Is this functionality within an enumerated type compiler dependant, an example is below to clarify.

enum FUNC_ERROR_CODE    
{
    FUNC_SUCCESS,
    FUNC_ERROR_1 = 24,
    FUNC_ERROR_2,
    FUNC_ERROR_3
}

Is it safe to assume that FUNC_ERROR_2 will have the value 25 and FUNC_ERROR_3 will have the value 26, regardless of compliler used.

I'm coding this so as a function can return an integer value, 0 is always success and any other value can signify failure.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

心碎的声音 2024-11-24 11:35:11

可以安全地假设:

  • 枚举声明 [dcl.enum]
    <块引用>

    枚举器列表中的标识符被声明为常量,并且可以出现在常量所在的任何位置
    必需的。带有 = 的枚举器定义为关联的枚举器提供了由
    常量表达式。常量表达式应为整型或枚举类型。 如果是第一个
    枚举器没有初始化器,相应常量的值为零。枚举器定义
    不带初始化器的情况下,枚举器将前一个枚举器的值增加一而获得的值。

但是如果您有类型安全枚举:使用它!不要依赖于整数的转换。

It is safe to assume that:

  • Enumeration Declaration [dcl.enum]

    The identifiers in an enumerator-list are declared as constants, and can appear wherever constants are
    required. An enumerator-definition with = gives the associated enumerator the value indicated by the
    constant-expression. The constant-expression shall be of integral or enumeration type. If the first
    enumerator has no initializer, the value of the corresponding constant is zero. An enumerator-definition
    without an initializer gives the enumerator the value obtained by increasing the value of the previous enumerator by one
    .

But if you have a typesafe enum: Use it! Don't fall back on converting to/from integers.

弥繁 2024-11-24 11:35:11

恕我直言,如果您的枚举必须具有固定值,那么明确地修复这些值,您将确定枚举中包含的值。

但是,如果您只需要相互比较枚举,请让编译器为您完成。
编写一些像这样的比较应该是安全的 if FUNC_ERROR_2 > > FUNC_ERROR_1 ...

IMHO, if your enums must have fixed values, then fix explicitely those values, you'll be sure of the values contained in your enums.

But if you only need to compare your enums each other, let the compiler do it for you.
It should be safe to write some comparisons like this if FUNC_ERROR_2 > FUNC_ERROR_1 ...

勿忘心安 2024-11-24 11:35:11

就 Visual Studio 2010 而言,它是安全的。查看 msdn 中的以下示例:

中的任何枚举数列表(包括第一个列表)可以初始化为其默认值以外的值。假设 Suit 的声明如下:

enum Suit {
   Diamonds = 5,
   Hearts,
   Clubs = 4,
   Spades
};

然后是方块、红心、梅花和黑桃的值 。分别是5、6、4和5,请注意,5被多次使用。

It is safe as far as Visual Studio 2010 goes. Check out the following example from msdn:

"Any enumerator in the list, including the first one, can be initialized to a value other than its default value. Suppose the declaration of Suit had been the following:

enum Suit {
   Diamonds = 5,
   Hearts,
   Clubs = 4,
   Spades
};

Then the values of Diamonds, Hearts, Clubs, and Spades would have been 5, 6, 4, and 5, respectively. Note that 5 is used more than once."

淑女气质 2024-11-24 11:35:11

我非常确定编译器必须为每个新值递增。

I'm pretty sure that the compiler is compelled to increment for each new value.

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