C
enum stuff q;
enum stuff {a, b=-4, c, d=-2, e, f=-3, g} s;
在作用域中s
用作具有完整类型的带符号整数的暂定定义的声明和q
在作用域中用作具有不完整类型的带符号整数的暂定定义的声明(解析为作用域中的完整类型,因为类型定义存在于作用域中的任何位置范围)(与任何暂定定义一样,标识符q
和s
可以使用相同类型的不完整或完整版本int
或enum stuff
多次声明,但只能在范围中定义一次,即int q = 3;并且只能在子范围内重新定义),并且仅在定义后可用)。同样,您只能enum stuff
在范围内使用一次完整类型,因为它充当类型定义。
enum stuff
还在文件作用域(在之前和之下使用)和前向类型声明(该类型enum stuff
可以有多个声明,但作用域中只有一个定义/完成)中提供了针对其的编译器枚举类型定义。。它也充当编译器指令,以在当前范围内a
用rvalue 0
,b
with -4
,c
with 5
,d
with -2
,e
with -3
,f
with -1
和g
with 代替-2
。现在,枚举常量将在定义之后应用,直到在不同枚举中的下一个重新定义(该枚举不能在同一作用域级别上)。
typedef enum bool {false, true} bool;
//this is the same as
enum bool {false, true};
typedef enum bool bool;
//or
enum bool {false, true};
typedef unsigned int bool;
//remember though, bool is an alias for _Bool if you include stdbool.h.
//and casting to a bool is the same as the !! operator
enum,struct和union共享的标记名称空间是分开的,并且必须在C中以type关键字(enum,struct或union)作为前缀,即after enum a {a} b
,enum a c
必须使用而不是a c
。因为标签名称空间与标识符名称空间是分开的,enum a {a} b
所以允许,但enum a {a, b} b
不是因为常量与变量标识符(标识符名称空间)在同一名称空间中。typedef enum a {a,b} b
也不允许,因为typedef名称是标识符名称空间的一部分。
C的类型enum bool
和常量遵循以下模式:
+--------------+-----+-----+-----+
| enum bool | a=1 |b='a'| c=3 |
+--------------+-----+-----+-----+
| unsigned int | int | int | int |
+--------------+-----+-----+-----+
+--------------+-----+-----+-----+
| enum bool | a=1 | b=-2| c=3 |
+--------------+-----+-----+-----+
| int | int | int | int |
+--------------+-----+-----+-----+
+--------------+-----+---------------+-----+
| enum bool | a=1 |b=(-)0x80000000| c=2 |
+--------------+-----+---------------+-----+
| unsigned int | int | unsigned int | int |
+--------------+-----+---------------+-----+
+--------------+-----+---------------+-----+
| enum bool | a=1 |b=(-)2147483648| c=2 |
+--------------+-----+---------------+-----+
| unsigned int | int | unsigned int | int |
+--------------+-----+---------------+-----+
+-----------+-----+---------------+------+
| enum bool | a=1 |b=(-)0x80000000| c=-2 |
+-----------+-----+---------------+------+
| long | int | long | int |
+-----------+-----+---------------+------+
+-----------+-----+---------------+------+
| enum bool | a=1 | b=2147483648 | c=-2 |
+-----------+-----+---------------+------+
| long | int | long | int |
+-----------+-----+---------------+------+
+-----------+-----+---------------+------+
| enum bool | a=1 | b=-2147483648 | c=-2 |
+-----------+-----+---------------+------+
| int | int | int | int |
+-----------+-----+---------------+------+
+---------------+-----+---------------+-----+
| enum bool | a=1 | b=99999999999 | c=1 |
+---------------+-----+---------------+-----+
| unsigned long | int | unsigned long | int |
+---------------+-----+---------------+-----+
+-----------+-----+---------------+------+
| enum bool | a=1 | b=99999999999 | c=-1 |
+-----------+-----+---------------+------+
| long | int | long | int |
+-----------+-----+---------------+------+
这在C语言中可以正常编译:
#include <stdio.h>
enum c j;
enum c{f, m} p;
typedef int d;
typedef int c;
enum c j;
enum m {n} ;
int main() {
enum c j;
enum d{l};
enum d q;
enum m y;
printf("%llu", j);
}
C ++
在C ++中,枚举可以具有类型
enum Bool: bool {True, False} Bool;
enum Bool: bool {True, False, maybe} Bool; //error
在这种情况下,常量和标识符都具有相同的类型bool,如果数字不能用该类型表示,则会发生错误。也许= 2,这不是一个笨蛋。另外,True,False和Bool不能小写,否则它们将与语言关键字冲突。枚举也不能具有指针类型。
枚举的规则在C ++中是不同的。
#include <iostream>
c j; //not allowed, unknown type name c before enum c{f} p; line
enum c j; //not allowed, forward declaration of enum type not allowed and variable can have an incomplete type but not when it's still a forward declaration in C++ unlike C
enum c{f, m} p;
typedef int d;
typedef int c; // not allowed in C++ as it clashes with enum c, but if just int c were used then the below usages of c j; would have to be enum c j;
[enum] c j;
enum m {n} ;
int main() {
[enum] c j;
enum d{l}; //not allowed in same scope as typedef but allowed here
d q;
m y; //simple type specifier not allowed, need elaborated type specifier enum m to refer to enum m here
p v; // not allowed, need enum p to refer to enum p
std::cout << j;
}
C ++中的枚举变量不再只是无符号整数等,它们还是枚举类型,只能在枚举中分配常量。但是,可以将其丢弃。
#include <stdio.h>
enum a {l} c;
enum d {f} ;
int main() {
c=0; // not allowed;
c=l;
c=(a)1;
c=(enum a)4;
printf("%llu", c); //4
}
枚举类
enum struct
等同于 enum class
#include <stdio.h>
enum class a {b} c;
int main() {
printf("%llu", a::b<1) ; //not allowed
printf("%llu", (int)a::b<1) ;
printf("%llu", a::b<(a)1) ;
printf("%llu", a::b<(enum a)1);
printf("%llu", a::b<(enum class a)1) ; //not allowed
printf("%llu", b<(enum a)1); //not allowed
}
范围解析运算符仍可用于非范围枚举。
#include <stdio.h>
enum a: bool {l, w} ;
int main() {
enum a: bool {w, l} f;
printf("%llu", ::a::w);
}
但是由于无法将w定义为范围内的其他内容,因此::w
和之间没有区别::a::w