类型定义枚举的问题.以及Visual Studio 2005中的错误

Problem with typedefing enums. And bug in visual studio 2005

本文关键字:2005 Studio 错误 Visual 以及 定义 枚举 问题 类型      更新时间:2023-10-16
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 的第一句话,现在说

类或命名空间成员或枚举器的名称可以在 :: 范围解析运算符之后引用

但它曾经说过

类或命名空间成员的名称可以在 :: 范围解析运算符之后引用

枚举

仅在周围的命名空间中"溢出",也就是struct A命名空间。一个简单的 typedef 不会对 struct B 产生相同的效果。


此外,如果启用警告级别 4 (/W4),则会收到以下警告:

警告 C4482:使用了非标准扩展:限定名称中使用的枚举"A::E"

不允许

使用A::E::XXX来引用当前标准中的枚举值。