Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access unnamed "enum class" encapsulated inside a class?

class A {
  public: enum class { HELLO, WORLD };
};

Having known that, inside a class, declaring a simple enum (rather than enum class) is a better idea, because it's already typed with the class identification. But still above statement is a valid C++0x signature. Now how to access an unnamed enum class outside ?

int i = A::HELLO; // error: ‘HELLO’ is not a member of ‘A’
like image 391
iammilind Avatar asked Jun 10 '11 03:06

iammilind


1 Answers

Actually, that is not valid. The C++0x FDIS says (9.2p1)

Except when used to declare friends (11.3) or to introduce the name of a member of a base class into a derived class (7.3.3), member-declarations declare members of the class, and each such member-declaration shall declare at least one member name of the class.

In your case, no enumerator name is introduced into the class' scope and no enumeration name is introduced either. So, no member name at all is introduced by that member-declaration.

EDIT: And actually, there's a more direct prohibition of the enumeration declaration. 7.2p2:

The optional identifier shall not be omitted in the declaration of a scoped enumeration.

like image 187
Johannes Schaub - litb Avatar answered Oct 13 '22 00:10

Johannes Schaub - litb