C++ declaring static enum vs enum in a class
static
cannot be applied to enum
declarations, so your code is invalid.
From N3337, §7.1.1/5 [dcl.stc]
The
static
specifier can be applied only to names of variables and functions and to anonymous unions ...
An enum
declaration is none of those.
You can create an instance of the enum
and make that static
if you want.
class Example
{
enum Items{ desk = 0, chair, monitor };
static Items items; // this is legal
};
In this case items
is just like any other static data member.
This is an MSVC bug; from the linked bug report it seems the compiler will allow both static
and register
storage specifiers on enum
declarations. The bug has been closed as fixed, so maybe the fix will be available in VS2015.
Related videos on Youtube
user3731622
Updated on June 07, 2020Comments
-
user3731622 almost 4 years
What's the difference between the
static enum
andenum
definitions when defined inside a class declaration like the one shown below?class Example { Example(); ~Example(); static enum Items{ desk = 0, chair, monitor }; enum Colors{ red = 0, blue, green }; }
Also, since we are defining types in a class, what do we call them? By analogy if I define a variable in a class, we call it a member variable.
-
swalog about 9 yearsWhat compiler are you using? It would surprise me if this compiles, as
static
wouldn't make much sense here. -
Dai about 9 yearsIf you're using C++11, consider using
enum class
. -
Collin Dauphinee about 9 yearsThe
static
specifier isn't valid in an enum declaration. This shouldn't compile. -
user3731622 about 9 years@swalog I'm using Visual Studio 2012. Do you know where I can find the compiler being used? I see the Platform Toolset is Visual Studio 2012 (v110).
-
user3731622 about 9 years@swalog I opened a Visual Studio command prompt and ran cl.exe. It says I'm using Microsost (R) C/C++ Optimizing Compiler Version 17.00.50727.1 for X86.
-
phuclv about 7 yearsPossible duplicate of C++: what does "static enum" mean
-
jww about 6 yearsAlso see What does “static enum” mean in C++?
-
-
Baum mit Augen about 9 yearsAn
enum
is no variable. -
Brian Bi about 9 yearsI do wonder, though, why
static
here isn't taken to apply to the set of 0 data members declared in this declaration. After all, if you didstatic enum Items { ... } item;
, it would be valid. -
Praetorian about 9 years@Brian I don't understand what you mean by set of 0 data members. There either is a data member or there isn't, and if there isn't then what would the storage specifier apply to?
-
NoseKnowsAll about 9 yearsWell because to have a set of 0 data members, you have to actually create a set to begin with. Using
enum
is like describing what a set would look like if you were to create one. It's not actually creating anything, though, as opposed to your example. -
Marcus Müller about 9 yearsyes, but a variable that stores the state encoded in an enum is.
-
Brian Bi about 9 yearsnvm, I found the right quote from the standard: "If a storage-class-specifier appears in a decl-specifier-seq, there can be no typedef specifier in the same decl-specifier-seq and the init-declarator-list of the declaration shall not be empty (except for an anonymous union declared in a named namespace or in the global namespace, which shall be declared static (9.5))."
-
Brian Bi about 9 yearsWhen you define an enum, struct, class, or union, the init-declarator-list is optional: if you include it then you declare 1 or more variables, and if you don't include it then you declare 0 variables. So it would seem that the grammar allows
static
to be applied to an enum declaration that doesn't declare any variables / data members---if an enum declaration can declare 0 variables of the type then why can't it declare 0 static variables of that type? But the standard explicitly forbids this, so I'm now satisfied that gcc and clang are correct. -
Baum mit Augen about 9 yearsBut he is not asking about those.
-
Marcus Müller about 9 years@BaummitAugen: 1. cool handle. ENTirely cool. 2. yep, realized my mistake.