The problem with typedefing enumerations. And a bug in visual studio 2005

struct A
{
    enum E
    {
        FIRST,
        SECOND
    };
};

struct B
{
    typedef A::E E;
};

int main()
{
    B::E e0 = A::FIRST;//OK (this case is clear for me)
    B::E e1 = A::E::FIRST;//OK (this case is clear for me as well)
    B::E e2 = B::FIRST;//Compile Error: FIRST is not member of B (Why isn't this allowed? Don't we lose meaning of typedef of enums in this case?)
    B::E e3 = B::E::FIRST;//Error of compiler (If there were no bug in visual studio 2005 compiler, would this code work?)
    return 0;
}

PS The question is in the code.

Update: Actually the bug is fixed in VS2010.

+3
source share
2 answers

After adding the missing semicolon to B::E e3 = B::E::FIRSTthe following:

In C ++ 03, only the first line ( B::E e0 = A::FIRST;) is correct , the other three are errors:

B::E e1 = A::E::FIRST; // error: ‘A::E’ is not a class or namespace
B::E e2 = B::FIRST; // error: ‘FIRST’ is not a member of ‘B’
B::E e3 = B::E::FIRST; // error: ‘B::E’ is not a class or namespace

In C ++ 0x, only the second line ( B::E e2 = B::FIRST;) is an error (FIRST is still not a member of B!), The other three are correct.

Not an answer to the question “why?”, Just pointing out that there are two different problems. The rationale for the problem that affects e1 and e3 may be explained in the C ++ 0x working papers.

3.4.3 [basic.lookup.qual]/1,

+5

"" , struct A. typedef struct B.


, 4 (/W4), :

C4482: : enum 'A:: E',

A::E::XXX .

+2

All Articles