枚举对象设置为不等于任何其各自的枚举常量的值 [英] Enumeration object set to a value not equal to any of its respective enumeration constants

查看:172
本文介绍了枚举对象设置为不等于任何其各自的枚举常量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果它被设置为不等于任何其各自的枚举常量的值不枚举对象有什么样的价值?

What value does an enumeration object have if it is set to a value not equal to any of its respective enumeration constants?

考虑以下code:

enum foobar{
    FOO = 1,
    BAR = 5
};

enum foobar baz = 5;
enum foobar qux = 42;

变量巴兹设置为整数值 5 ,而变量 qux 设置为整数值 42

The variable baz is set to the integer value 5, while the variable qux is set to the integer value 42.

我怀疑变量巴兹将持有的价值 BAR ,但我不确定变量 qux 。没有枚举常量被分配值 42 ,所以当枚举foobar的变量设置为这样的值,会发生什么?

I suspect the variable baz will hold the value BAR, but I'm unsure about the variable qux. No enumeration constant was assigned the value 42, so what happens when an enum foobar variable is set to such a value?

是在 C99 标准明确患得患失?

Is the C99 standard explicit about the outcome?

推荐答案

C99标准草案似乎并不限制枚举承担其成员全部由值,但是它不说,基本型普查员应能重新$ p $的psenting值其所有成员。这是覆盖在部分 6.7.2.2 枚举符的:

The C99 draft standard does not seem to restrict an enumerator to take on values of its members exclusively but it does say that the enumerators underlying type shall be capable of representing the values of all its members. This is covered in section 6.7.2.2 Enumeration specifiers:

每个枚举类型应与字符,有符号整数兼容
  型,或无符号整数类型。类型的选择是
  实现定义, 110),但应能重新presenting的
  枚举的所有成员的值。

Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined,110) but shall be capable of representing the values of all the members of the enumeration.

所以你将依托实现定义的行为,如果你使用大于成员定义值。在整数签署的情况下的溢出会导致不确定的行为按节 5 它说:

and so you will be relying on implementation defined behavior if you use a value greater then the members define. In the case of signed integer overflow leads to undefined behavior as per section 5 which says:

如果一个企业的评估过程中出现的异常情况
  前pression(即,如果结果不是数学上定义或
  未在其类型重新presentable值的范围),则该行为
  未定义。

If an exceptional condition occurs during the evaluation of an expression (that is, if the result is not mathematically defined or not in the range of representable values for its type), the behavior is undefined.

典型地,我们看到枚举当它们被用于重新present比特字段被分配的那些成员中指定的以外的值。

Typically we see enums being assigned values outside of those specified by their members when they are used to represent bit fields.

这篇关于枚举对象设置为不等于任何其各自的枚举常量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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