Answered Using :: to select enum options = Warning ?

  • Thursday, May 03, 2012 1:44 PM
     
      Has Code
    Why do you get a Warning when you use the scope operator :: to select enum options?

    Using :: activiates intellisense which shows you what enum options are available... saving you having to remember or go back and check

    enum myColor {Red, Green, Blue};
    int main()
    {
    	myColor myC = myColor::Red; 	// gets Warning
    	//myColor myC = Red;		// gets  no Warning
    	cout <<myC<<endl;
    	getchar();
    	return 0;
    }
    


All Replies

  • Thursday, May 03, 2012 1:53 PM
     
     Answered

    You get the warning because it is technically illegal to specify it that way in C++03.  Enum items are exported to the enclosing namespace, rather than placed in one private to that enum.  Likely due to C++/CLI, the Microsoft C++ compiler accepts that syntax, although it generates a warning. 

    C++0x adds enum classes which do not export their items to the enclosing namespace and allow you to specify the underlying data type.  This is fully supported in Visual C++ 11, and partially supported in 10.

    Personally I don't have any need for portability most of the time so I disable the warning and code like you do.

  • Thursday, May 03, 2012 1:58 PM
     
     Proposed
    Why do you get a Warning when you use the scope operator :: to select enum options?
     
    Using :: activiates intellisense which shows you what enum options are available... saving you having to remember or go back and check
     
    enum myColor {Red, Green, Blue};
    int main()
    {
        myColor myC = myColor::Red; // gets Warning
        //myColor myC = Red; // gets no Warning
        cout <<myC<<endl;
        getchar();
        return 0;
    }
    Enums in standard C++ do not define a scope. Enum class in C++/CLI does.
     

    David Wilkinson | Visual C++ MVP
  • Thursday, May 03, 2012 1:59 PM
     
     Answered Has Code

    This is a compiler extension as the warning tells you. Basically, for standard C/C++ enumerations aren't scoped, that means normally when you define an enumeration, it wouldn't create a namespace for it and it wouldn't create values for it inside that namespace. The syntax enumname::value only works because the VC compiler creates the namespace and values inside the namespace. If you were to try to compile this on another compiler, or disable the VC language extensions then this would result in:

    1>d:\test02\test02\main.cpp(8): error C2825: 'myColor': must be a class or namespace when followed by '::'
    1>d:\test02\test02\main.cpp(8): error C2275: 'myColor' : illegal use of this type as an expression
    1>          d:\test02\test02\main.cpp(4) : see declaration of 'myColor'
    1>d:\test02\test02\main.cpp(8): error C2275: 'myColor' : illegal use of this type as an expression
    1>          d:\test02\test02\main.cpp(4) : see declaration of 'myColor'
    1>d:\test02\test02\main.cpp(8): error C2143: syntax error : missing ';' before 'Red'

    So this is giving you a warning because it is a VC only feature and may not work on other compilers.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

  • Thursday, May 03, 2012 2:28 PM
     
     

    OK Guys, thanks... But...

    depending on how big the project is and ... how many items are defined in enum and ... how hard it is to go and locate the enum definition in the code so you can see the list of available items...

    This is a place where intellisense comes in very handy... i.e. it shows you the available options so you don't have to remember them or go find them...

    But intellisense is a Microsoft feature so I guess it would have not been appropriate for the C++ standards folks consider this convenience...

    Is there another way to have intellisense show you the list of options contained in an enum definition list?

    I guess you can always use the scope operator to 'see what's enumerated... and then remove it so the code as it should be...

    (Or if your only writing for MS platforms do as SimonRev does and turn off or ignore the warnings)

    Thanks for the help !

  • Thursday, May 03, 2012 2:42 PM
     
     

    Well, after using Visual Studio 2005 and 2008, I learnt not to rely on intellisense (it was so slow and inaccurate).

    The other thing to remember is that enum is a C feature that C++ supports. The C language doesn't have the scope operator so this is even out of the C++ standard writer's hands.

    For how hard is it to see the list of available items. Visual Studio has go to definition/go to decleration on the context menu. Normally you remember your own types as you keep using them. At the worst you could always make a note somewhere the types you use or put your types into one header inside your project.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

  • Thursday, May 03, 2012 4:59 PM
     
     Proposed

    On 03/05/2012 16:28, Mel_3 wrote:

    depending on how big the project is and ... how many items are defined in enum and ... how hard it is to go and locate the enum definition in the code so you can see the list of available items...

    This is a place where intellisense comes in very handy... i.e. it shows you the available options so you don't have to remember them or go find them...

    But intellisense is a Microsoft feature so I guess it would have not been appropriate for the C++ standards folks consider this convenience...
    Is there another way to have intellisense show you the list of options contained in an enum definition list?

    Considering the scope problem of enum's, I tend to use a common prefix for the enum items, e.g.

    enum Colors
    {
        Color_Red,
        Color_Green,
        Color_Blue
    //    ... etc.
    };
    

    Then with Visual Assist X, when I type "Color_" a list with all the enum items is displayed in the editor (I'm not sure VS2010 IDE can do that without VAX...).

    Giovanni

  • Friday, May 04, 2012 4:44 AM
     
     
    Mel_3 wrote:
    >
    >But intellisense is a Microsoft feature so I guess it would have not
    >been appropriate for the C++ standards folks consider this convenience...
     
    The standard came along WAY before Microsoft had this in intellisense.
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.
     

    Tim Roberts, VC++ MVP Providenza & Boekelheide, Inc.