其他分享
首页 > 其他分享> > c – typedefing枚举问题.和visual studio 2005中的bug

c – typedefing枚举问题.和visual studio 2005中的bug

作者:互联网

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;
}

附:代码中的问题.

更新:实际上该错误已在VS2010中修复.

解决方法:

在B :: E e3 = B :: E :: FIRST中添加缺少的分号后,以下内容成立:

在C 03中,只有第一行(B :: E e0 = A :: FIRST;)是正确的,其他三个是错误:

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

在C 0x中,只有第二行(B :: E e2 = B :: FIRST;)是一个错误(FIRST仍然不是B的成员!),其他三个是正确的.

不是“为什么?”的答案,只是指出手头有两个不同的问题.影响e1和e3的问题的基本原理可能在C 0x工作文件中有所解释.

改变是3.4.3 [basic.lookup.qual] / 1的第一句话,现在说

The name of a class or namespace member or enumerator can be referred to after the :: scope resolution operator

但它曾经说过

The name of a class or namespace member can be referred to after the :: scope resolution operator

标签:c,enums,visual-studio,visual-studio-2005
来源: https://codeday.me/bug/20190730/1578434.html