Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope resolution operator on enums a compiler-specific extension?

Tags:

c++

standards

On this question, there's an answer that states:

You can use typedef to make Colour enumeration type accessible without specifying it's "full name".

typedef Sample::Colour Colour;
Colour c = Colour::BLUE;

That sounds correct to me, but someone down-voted it and left this comment:

Using the scope resolution operator :: on enums (as in "Colour::BLUE") is a compiler-specific extension, not standard C++

Is that true? I believe I've used that on both MSVC and GCC, though I'm not certain of it.

like image 325
Head Geek Avatar asked Jan 14 '09 01:01

Head Geek


4 Answers

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.

like image 144
Ferruccio Avatar answered Oct 21 '22 16:10

Ferruccio


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.

like image 40
Drew Dormann Avatar answered Oct 21 '22 18:10

Drew Dormann


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.

like image 31
AnT Avatar answered Oct 21 '22 18:10

AnT


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

like image 23
Todd Gardner Avatar answered Oct 21 '22 18:10

Todd Gardner