18

What's the difference between the static enum and enum 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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user3731622
  • 4,844
  • 8
  • 45
  • 84
  • 1
    What compiler are you using? It would surprise me if this compiles, as `static` wouldn't make much sense here. – swalog Feb 24 '15 at 22:18
  • If you're using C++11, consider using `enum class`. – Dai Feb 24 '15 at 22:18
  • The `static` specifier isn't valid in an enum declaration. This shouldn't compile. – Collin Dauphinee Feb 24 '15 at 22:19
  • @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 Feb 25 '15 at 00:32
  • @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. – user3731622 Feb 25 '15 at 00:39
  • Possible duplicate of [C++: what does "static enum" mean](http://stackoverflow.com/questions/4971436/c-what-does-static-enum-mean) – phuclv Apr 19 '17 at 03:59
  • Also see [What does “static enum” mean in C++?](https://stackoverflow.com/q/4971436/608639) – jww Feb 20 '18 at 17:47

2 Answers2

17

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.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • 1
    I 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 did `static enum Items { ... } item;`, it would be valid. – Brian Bi Feb 24 '15 at 22:25
  • @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? – Praetorian Feb 24 '15 at 22:27
  • Well 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. – NoseKnowsAll Feb 24 '15 at 22:28
  • nvm, 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 Feb 24 '15 at 22:32
  • When 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. – Brian Bi Feb 24 '15 at 22:35
0

static is a C++ storage specifier. It means the value of this member of the class is the same for all instances of the class. Nothing special about enums here.

EDIT: Even the static tag wiki has an explanation. On exactly this topic.

EDIT2: Oh, I've misread your code. There's no static enum. You can have a static variable of an enum type that holds a value.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94