AdaRaider
AdaRaider

Reputation: 1146

Warning C4481 nonstandard extension used in enum 'MyEnum' used in qualified name

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.

Upvotes: 1

Views: 1529

Answers (2)

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
};

Upvotes: 3

Bob Shaffer
Bob Shaffer

Reputation: 655

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.

Upvotes: 3

Related Questions