씨
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 및 단지 subscope 재정의 될 수 있고, 정의 후에 만 사용 가능). 또한 enum stuff유형 정의 역할을하기 때문에 범위에서 한 번만 전체 유형을 사용할 수 있습니다 .
컴파일러 열거 형 정의 enum stuff는 파일 형식 (앞과 아래에서 사용 가능)과 순방향 형식 선언에도 존재합니다 (유형 enum stuff은 여러 선언을 가질 수 있지만 범위에서 하나의 정의 / 완료 만 가능하며 하위 범위에서 재정의 할 수 있음). . 또한 대체 할 수있는 컴파일러 지시자 역할 ar- 수치와 0, b와 -4, c와 5, d와 -2, e와 -3, f와 -1와 g함께 -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
열거 형, 구조체 및 조합에 의해 공유 태그 네임 스페이스는 별도이며, 이후 C, 즉의 유형 키워드 (열거 형, 구조체 또는 조합) 접두어로 지정해야합니다 enum a {a} b, enum a c사용하지되어야합니다 a c. 태그 네임 스페이스는 식별자 네임 스페이스와 별개 enum a {a} b이므로 허용되지만 enum a {a, b} b상수가 변수 식별자 인 식별자 네임 스페이스와 동일한 네임 스페이스에 있기 때문이 아닙니다. typedef enum a {a,b} btypedef-names는 식별자 네임 스페이스의 일부이므로 허용되지 않습니다.
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