问题
I did a search for this question thinking that somebody must have asked it before. I did not turn up any results, so if it has been, please post the link and feel free to close the question.
I ran across this code in EASTL:
enum : size_type { // size_type = size_t
npos = (size_type)-1,
kMaxSize = (size_type)-2
};
I have never encountered an enum
declaration like that. What does the :
do in this case?
回答1:
This is a Microsoft extension that lets you choose the base type of the enum values. For example, this lets you specify that values are unsigned (Microsoft's compilers usually choose signed by default) or that they only occupy 8 or 16 bits (Microsoft normally defaults to 32 bits).
The syntax is documented here: http://msdn.microsoft.com/en-us/library/2dzy4k6e(v=VS.100).aspx but I'm not able to find official documentation of what it actually does.
C++11 adds a similar feature, but with slightly different syntax. In C++11 you'd write it like this:
enum class MyEnum : size_type { .. values .. };
回答2:
In C++0x, you can specify the underlying type for the enum. In this case, it will be size_type
.
(And it may be supported as an extension in other places prior to C++0x, obviously.)
来源:https://stackoverflow.com/questions/6496320/what-does-this-colon-do-in-an-enum-declaration