Using enum inside types - Compiler warning C4482 C

2019-01-22 00:50发布

I am using fully qualified name of the enum inside a method in one of my class. But I am getting compiler warning which says "warning C4482: nonstandard extension used: enum 'Foo' used in qualified name". In C++, do we need to use enums without the qualified name? But IMO, that looks ugly.

Any thoughts?

8条回答
Explosion°爆炸
2楼-- · 2019-01-22 01:28

namespace Company
{
    typedef int Value;
    enum
    {
        Microsoft= 0,
        APPLE = 1, 
    };
};

namespace Fruit
{
    typedef int Value;
    enum
    {
        ORANGE = 0,
        BANANA = 1,
        APPLE = 2, 
    };
};

...

Fruit::Value f = Fruit::BANANA; //No warning
Company::Value f = Company::APPLE; //is different value then Fruit::APPLE

This works on GCC and MS compiler and Mac. And the advantage is that you can use namespace operator and pass conflicts. The little disadvantage is that instead of Fruit, you have to write Fruit::Value. it is more useful in large project when you don't know what enums are in other class.

If it is possible to use C++11 instead, it is much more simple, because the enum::namespace syntax is then possible.

查看更多
贼婆χ
3楼-- · 2019-01-22 01:28

I had the same problem and I'm not using C++ 11 yet. I much prefer fully qualified namespaces myself too.

I disabled this particular warning. I'm sure people will dislike the idea but some may be thankful..

#pragma warning( disable : 4482 )
查看更多
萌系小妹纸
4楼-- · 2019-01-22 01:34

The cleanest way I've found to do this is defining the enum as such

namespace Samples
{
    enum Value
    {
        Sample1,
        Sample2,
        Sample3
    };
}
typedef Samples::Value Sample;

Then in function and variable definitions you can use the typedef:

void Function(Sample eSample);
Sample m_eSample;

And in your .cpp file you can use the namespace to assign variables:

void Function(Sample eSample)
{
    m_eSample = Samples::Sample1;
    eSample = Samples::Sample2;
}
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-22 01:36

Yes. Conceptually enum defines a type, and the possible values of that type. Even though it seems natural, to define enum foo { bar, baz }; and then refer to foo::baz is the same as referring to int::1.

查看更多
\"骚年 ilove
6楼-- · 2019-01-22 01:38

While sth does answer the question, it didn't address how I've always used enums. Even though they're just more or less names for numbers, I've always used them to define types that can only have certain values.

If the enum is part of the class, then that helps consumers clearly identify an enum reference:

class Apple {
  enum Variety {
    Gala,
    GoldenDelicious,
    GrannySmith,
    Fuji
  }
  ...
};

Then consumers would be able declare instances of the enum, pass as parameters, and qualify them when referencing one of the types.

unsigned int GetCountOfApples( Apple::Variety appleVariety );
...
fujiCnt = GetCountOfApples( Apple::Fuji );

Sometimes you want an enum outside of a class or two enums in the same class, and you can do something like what Poy had. You won't be able to reference the enum type though, so just name it.

namespace Color {
  enum ColorEnum {
    Blue,
    Red,
    Black
  };

Now using the enum and values would work like:

Color::ColorEnum firstColor = Color::Blue;
Color::ColorEnum secondColor = Color::Red;

if( firstColor == secondColor )
....

Now if there happens to be different enums with the same name in them, they will always be qualified with what type they are. Then you could handle what gamblor is asking about.

BananaColorEnum banCol = BananaColor::Yellow;
TomatoColorEnum tomCol = TomatoColor::Yellow;
查看更多
Fickle 薄情
7楼-- · 2019-01-22 01:40

To make it clean, replace:

enum Fruit {

    ORANGE = 0,
    BANANA = 1

};

with

namespace Fruit {

    enum { //no enum name needed

        ORANGE = 0,
        BANANA = 1

    };

};

...

int f = Fruit::BANANA; //No warning
查看更多
登录 后发表回答