C++ declaring static enum vs enum in a class

30,532

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.

Share:
30,532

Related videos on Youtube

user3731622
Author by

user3731622

Updated on June 07, 2020

Comments

  • user3731622
    user3731622 almost 4 years

    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.

    • swalog
      swalog about 9 years
      What compiler are you using? It would surprise me if this compiles, as static wouldn't make much sense here.
    • Dai
      Dai about 9 years
      If you're using C++11, consider using enum class.
    • Collin Dauphinee
      Collin Dauphinee about 9 years
      The static specifier isn't valid in an enum declaration. This shouldn't compile.
    • user3731622
      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
      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
      phuclv about 7 years
      Possible duplicate of C++: what does "static enum" mean
    • jww
      jww about 6 years
  • Baum mit Augen
    Baum mit Augen about 9 years
    An enum is no variable.
  • Brian Bi
    Brian Bi about 9 years
    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.
  • Praetorian
    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
    NoseKnowsAll about 9 years
    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.
  • Marcus Müller
    Marcus Müller about 9 years
    yes, but a variable that stores the state encoded in an enum is.
  • Brian Bi
    Brian Bi about 9 years
    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
    Brian Bi about 9 years
    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.
  • Baum mit Augen
    Baum mit Augen about 9 years
    But he is not asking about those.
  • Marcus Müller
    Marcus Müller about 9 years
    @BaummitAugen: 1. cool handle. ENTirely cool. 2. yep, realized my mistake.