Scope resolution operator on enums a compiler-specific extension?

这一生的挚爱 提交于 2019-11-29 14:05:21

I tried the following code:

enum test
{
    t1, t2, t3
};

void main() 
{
    test t = test::t1;
}

Visual C++ 9 compiled it with the following warning:

warning C4482: nonstandard extension used: enum 'test' used in qualified name

Doesn't look like it's standard.

That is not standard.

In C++11, you will be able to make scoped enums with an enum class declaration.

With pre-C++11 compilers, to scope an enum, you will need to define the enum inside a struct or namespace.

In standard c++, things to the left of "::" must be a class or namespace, enums don't count.

This is not allowed in C++98. However, staring from C++11 you can optionally use scope resolution operator with "old-style" enums

enum E { A };

int main()
{
  A;    // OK
  E::A; // Also OK
}

Both ways of referring to A are correct in C++11 and later.

What you can do to get around it is to create a namespace that's the same name as the enumeration. That will effectively add the enumeration values into their own scope and you can use the name of the enumeration/namespace to refer to them. Of course it only works for enumerations that would otherwise exist in the global (or another namespace) scope.

There's also an article on this issue somewhere.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!