1

I am trying to remove the warnings from a class I am working on.

The warning is as follows:

Warning C4482: nonstandard extension used: enum 'MyEnum' used in qualified name

I understand that the warning is caused by attempting to access one of the values in the enum like so:

//Declared in a C header file.
enum MyEnum
{
    Value1,
    Value2,
    Value3 
};

//Exists in some other .cpp file.
void SomeFunc()
{
    MyEnum::Value1; //Generates warning C4482
}

FYI: SomeFunc() exists in a .cpp file but the enum is declared in a C header file.

So one way I can remove the warning is to replace MyEnum::Value1 with just Value1. However I would much rather refer to the enum values with MyEnum::Value1 as I like that it is more explicit.

So if I was using just C++ I could change the enum like so:

namespace MyEnum
{
    enum 
    {
        Value1,
        Value2,
        Value3 
    };
}

However the enum exists in a C header file and so I can't wrap the enum in a namespace. I also can't move the enum in to a C++ header file as other files are already dependent on the enum.

One way I considered was to wrap the enum in a struct:

struct MyEnum
{
   enum Type
   { 
       Value1,
       Value2,
       Value3
   };
 };

Which would allow me to access the values with MyEnum::Value1 without raising the warning.

However, is there a better way to achieve this?

Furthermore I've also had situations where the enum existed in a C++ file but it was scoped to a class:

class MyClass
{
    enum MyEnum
    {
        Value1,
        Value2,
        Value3
    };
};

I don't want to move it out of the class because the class provides the encapsulation for the enum but I also can't wrap the enum in a namespace because namespaces are not allowed in the class declaration. Is there a better way to implement the same behavior (MyEnum::Value1) in this situation without raising the same warning?

FYI2: I am limited to implementations that are allowed by vc10.

Lundin
  • 195,001
  • 40
  • 254
  • 396
AdaRaider
  • 1,126
  • 9
  • 20
  • 1
    maybe you could do `enum class MyEnum` when it's C++ – M.M Feb 04 '19 at 03:33
  • Yeah I think enum class can be used from within another class which is good to know for the second case. However I'm not sure if it's supported by vc10, I'll have to check that and post when I find out. – AdaRaider Feb 04 '19 at 03:40
  • My findings would suggest that enum class is not supported in vc10, but still, good to know. – AdaRaider Feb 04 '19 at 05:35

2 Answers2

3

I don't believe there is any :: operator in standard C, so even in a struct the enum values would most likely just be global symbols. With it inside a struct, class, or namespace like you showed, in C++, it would be accessible using that operator.

Bob Shaffer
  • 635
  • 3
  • 13
  • Agreed, if I'm using the enum from a file compiled as C code, there's nothing I can do about having to refer to the enum values that way. But when using C++ it would be nice to have the option without it raising the warning. – AdaRaider Feb 04 '19 at 03:31
  • If it's in a header, it only gets compiled when the source file referencing it is compiled. There are some preprocessor directives I have seen that you can use to make it so that it gets wrapped in a namespace if it's being used in C++, but I'm not sure if any are portable. – Bob Shaffer Feb 04 '19 at 03:41
3

Qualifying an enumerator by the enumeration name is standard C++ only since C++11.

Before that, you could not write MyEnum::Value1 in standard C++. Microsoft however, allowed it as an extension even before it was standardized.

VC10 is warning you that your qualification is making use of the extension and not something standard. If memory serves C++11 is not the the default standard used by VC10.

Since this is meant to be a C header, and enumerator names are always members of the enclosing scope, you have no recourse but "qualifying" them in the C way

enum MyEnum { 
    MyEnum_Value1
};
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Yeah I realise it's not the default standard and therefore the warning is completely justified but I can't upgrade to vc14 either (legacy issues). – AdaRaider Feb 04 '19 at 05:24