enum的问题!!!!!!请大家给个提示。谢谢

KDE 2007-10-07 10:41:46
最近无意中使用MSDN的时候,我发现枚举的定义好像与以往不同:
VS2005帮助中说明枚举可以申明如下:
enum [tag] [: type] {enum-list} [declarator]; // for definition of enumerated type
enum tag declarator; // for declaration of variable of type tag

我马上在VC(2005)中试了一下:

enum Test : unsigned char
{
TEST_VALUE1 = 255,
TEST_VALUE2
};

编译之后给出了如下警告:
d:\tmp\test2\test2\main.cpp(9) : warning C4340: 'TEST_VALUE2' : value wrapped from positive to negative value
d:\tmp\test2\test2\main.cpp(9) : warning C4309: 'initializing' : truncation of constant value
d:\tmp\test2\test2\main.cpp(9) : warning C4369: 'TEST_VALUE2' : enumerator value '256' cannot be represented as 'unsigned char', value is '0'

除了感叹VC8的强大检查能力之外,我没有在任何标准文档中找到关于枚举可以指定基本类型的。
而且我在msdn中并没有看见这个特性被称作VC的扩展。

小弟在此请问各位大侠,这个语法(特性)是什么时候加入标准的,在那个文档或书中有记载?

或者如果没有加入标准,那又是什么时候作为VC扩展特性的,有没有相关文档记载?

谢谢!!!
...全文
195 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ckt 2007-10-08
  • 打赏
  • 举报
回复
学习..
jixingzhong 2007-10-08
  • 打赏
  • 举报
回复
上述是标准常用方式。


引用自: MSDN
num [tag] [: type] {enum-list} [declarator]; // for definition of enumerated type
enum tag declarator; // for declaration of variable of type tag

Parameters

tag

The type name given to the enumeration.

type

The underlying type of the enumeration identifiers. See Remarks.

enum-list

List of the enumerators contained by the enumeration.

declarator

Declarator list specifying the names of the enumeration. See Overview of Declarators for more information.

RemarksRemarks

By default, the first enumerator has a value of 0, and each successive enumerator is one larger than the value of the previous one, unless you explicitly specify a value for a particular enumerator. Enumerators needn't have unique values within an enumeration. The name of each enumerator is treated as a constant and must be unique within the scope where the enum is defined. An enumerator can be promoted to an integer value. However, converting an integer to an enumerator requires an explicit cast, and the results are not defined if the integer value is outside the range of the defined enumeration.

type is the underlying type of the identifiers. This can be any scalar type, such as 【signed or unsigned versions】 of 【int, short, or long. bool or char】 is also allowed.
...

该方式定义,用VS2005
jixingzhong 2007-10-08
  • 打赏
  • 举报
回复
enum [tag] {enum-list} [declarator];
visame 2007-10-08
  • 打赏
  • 举报
回复
"这个语法(特性)是什么时候加入标准的,在那个文档或书中有记载?

或者如果没有加入标准,那又是什么时候作为VC扩展特性的,有没有相关文档记载?"
个人观点如下:
这个可能是VC8的编译器自己设定的。并没有加入标准。

这个就好像C89和C99中都没有bool这个变量类型,但是很多编译器都支持bool,这是编译器方便用户设定的。不属于标准范围之列。
欢迎大家批评指正!
wjvlangz 2007-10-07
  • 打赏
  • 举报
回复
没见过,纯接分
星羽 2007-10-07
  • 打赏
  • 举报
回复
c++标准文档里好像没有这个,只是提了下 Underlying Type of an Enum 的 default 是 int


具体不及的了

65,199

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧