c++classenumsstaticnested

C++ declaring static enum vs enum in a class


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.


Solution

  • 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.