[英]Is valid casting an `enum` to other `enum` in [C]?
I have two enums. 我有两个枚举。
enum A { A1=1, A2=2 }
enum B { B1=1, B2=2 }
Is this valid by standard of C? 这是否符合C的标准?
A a = A1;
B b = a;
(compiled well with Clang, but I can't sure this is standard or extension behavior) (与Clang编译良好,但是我不确定这是标准行为还是扩展行为)
It's valid by the standard, but the C99 spec mentions that some implementations may generate a warning: 按照标准,它是有效的,但是C99规范提到某些实现可能会生成警告:
An implementation may generate warnings in many situations, none of which are specified as part of this International Standard.
一个实现可能在许多情况下生成警告,但没有一个警告内容被指定为该国际标准的一部分。 The following are a few of the more common situations.
以下是一些较常见的情况。
- A value is given to an object of an enumerated type other than by assignment of an enumeration constant that is a member of that type, or an enumeration variable that has the same type, or the value of a function that returns the same enumerated type (6.7.2.2).
除了通过分配属于该类型成员的枚举常量,具有相同类型的枚举变量或返回相同枚举类型的函数的值( 6.7.2.2)。
I believe that in C enums are basically int
s with personality. 我相信,在C枚举基本
int
s的个性。 (This contrasts with C++ where they are full fledged types.) So, assigning different enum
s is effectively still just working with int
s, so it is legal. (这与它们是完全类型的C ++形成对比。)因此,分配不同的
enum
实际上仍然仅与int
一起使用,因此是合法的。 However, I'm not saying this is recommended :) 但是,我并不是说建议这样做:)
It is valid Standard C but it's a bad idea. 它是有效的标准C,但不是一个好主意。 Note that it is not valid C++.
请注意,它不是有效的C ++。
valid - enums are language features which are intended to help the developer. 有效-枚举是旨在帮助开发人员的语言功能。 If you don't specify enum item values their values it's generated automatically.
如果您不指定枚举项目值,则它们的值会自动生成。 From another language .
从另一种语言 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.