“静态枚举”在 C++ 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4971436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "static enum" mean in C++?
提问by Marcin K
I recently came across this:
我最近遇到了这个:
static enum Response{
NO_ERROR=0,
MISSING_DESCRIPTOR,
...
};
It compiles and works under Microsoft VS2005. However, I'm not sure what the 'static' modifier is supposed to do. Is it any different from the following?
它在 Microsoft VS2005 下编译和运行。但是,我不确定“静态”修饰符应该做什么。和下面的有什么不同吗?
enum Response {
NO_ERROR=0,
MISSING_DESCRIPTOR,
...
};
回答by James McNellis
That exact code, with just the ellipsis removed, is not valid C++. You can't use the static
storage class specifier in an enum
declaration; it doesn't make any sense there (only objects, functions, and anonymous unions can be declared static
).
那个确切的代码,只是去掉了省略号,不是有效的 C++。不能static
在enum
声明中使用存储类说明符;在那里没有任何意义(只能声明对象、函数和匿名联合static
)。
You can, however, declare an enum
and a variable all in one declaration:
但是,您可以enum
在一个声明中同时声明一个和一个变量:
static enum Response {
NO_ERROR = 0,
MISSING_DESCRIPTOR
} x;
The static
here applies to x
and it is effectively the same as if you said:
在static
这里适用于x
和你说,这实际上是相同的:
enum Response {
NO_ERROR = 0,
MISSING_DESCRIPTOR
};
static Response x;
回答by GrahamS
Surprisingly you can put other decl-specifiersin there too.
This compiles fine in VS2008:
令人惊讶的是,您也可以在其中放置其他声明说明符。
这在 VS2008 中编译得很好:
auto const enum TestEnum {
Why,
Does
};
register volatile enum TestEnum2 {
This,
Work
};
But it makes no sense at all :)
但这根本没有意义:)
I suspect the problem here is in the parsing, because code like this:
我怀疑这里的问题在于解析,因为这样的代码:
enum TestEnum3 { Hello, World }; // Define enum
enum TestEnum3 x = World; // Use enum
Could also be written as:
也可以写成:
enum TestEnum3 { Hello, World } x = World; // Define and use enum.
Interestingly, I notice if you do this in VS2008:
有趣的是,我注意到你是否在 VS2008 中这样做:
enum TestEnum3 { Hello, World };
const enum TestEnum3 e3 = World;
const enum TestEnum4 { F, M, L } e4 = F;
e3 = Hello; // error C2166: l-value specifies const object (Good!)
e4 = M; // NO ERROR here though - why?
So they are not equivalent as in the TestEnum4
case it seems to be throwing away the const
decl-specifier. All very odd.
所以它们并不等同,因为TestEnum4
它似乎丢弃了const
decl-specifier。都很奇怪。
回答by Nawaz
static enum Response { /*... */ };
You cannot define static
enum in C++. static
can only be the variableof the enum, not the typeitself!
您不能static
在 C++ 中定义枚举。 static
只能是枚举的变量,不能是类型本身!
Compiling your code with GCC
version 4.3.4
, it gives this error:
用GCC
version编译你的代码4.3.4
,它给出了这个错误:
prog.cpp:7: error: a storage class can only be specified for objects and functions
prog.cpp:7: 错误:只能为对象和函数指定存储类
See yourself online at ideone: http://www.ideone.com/cI1bt
在 ideone 上在线查看您自己:http: //www.ideone.com/cI1bt
I think that says it all.
我认为这一切都说。
--
——
However, if you want to limit the typeenum Response
in it's own translation unit, then you can use unnamed namespace. Have a look at this topic:
但是,如果您想限制它自己的翻译单元中的类型enum Response
,那么您可以使用未命名的命名空间。看看这个主题:
回答by Latency
In C#:
在 C# 中:
The ';' is optional for backwards compatibility after the enum block. It does notallow for such semantics in that language for named types. static, public, etc. have special consideration. Namespace cannot contain members such as fields or methods.
这 ';' 是可选的,以便在 enum 块之后向后兼容。它并没有允许在该语言名为类型,语义。static、public 等有特殊考虑。命名空间不能包含成员,如字段或方法。
Requires tag:
需要标签:
ArgTypes var = ArgTypes.CUT;
In C/C++:
在 C/C++ 中:
Requires ';' at the end of enum block. For global namespace variables, enumerations, etc. staticis by default.
需要';' 在枚举块的末尾。对于全局命名空间变量、枚举等 ,默认情况下是静态的。
int type;
typedef enum {
TOKENIZE,
CUT
} ArgTypes;
type = TOKENIZE; /* <ArgTypes>::TOKENIZE */
type = ArgTypes::CUT;
// Recommended Use
enum ArgTypes {
TOKENIZE,
CUT
}; /* Same as above */
enum Test {
TOKENIZE,
CUT
} ArgTypes;
type = ArgTypes::TOKENIZE;
type = CUT; /* Assign type => <Test>.CUT */
type = Test::CUT;
enum {
TOKENIZE,
CUT
} ArgTypes; /* Unamed.. requires tag */
type = TOKENIZE; /* <unamed>.TOKENIZE=0 => #define TOKENIZE 0*/
type = ArgTypes::TOKENIZE; /* ** ERROR ** */
回答by JonH
Im not sure why static was used or why it even compiles. Should just be enum Response. Enumerators are not static data.
我不确定为什么使用 static 或者为什么它甚至编译。应该只是枚举响应。枚举数不是静态数据。