类型定义枚举的问题.以及Visual Studio 2005中的错误
Problem with typedefing enums. And 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;
}
附言代码中的问题。
更新:实际上该错误已在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
来引用当前标准中的枚举值。
相关文章:
- 无法在Visual Studio 2005中构建Qt项目
- 是否可以在Windows 8环境上在Visual Studio 2005上运行VC ,Win32应用程序
- 如何使用 Visual Studio 2005 获取两个不同 c++ 程序之间的返回值
- 将 coverity SA 与 Visual Studio 2005 集成的步骤以及如何运行相同的步骤
- 用于在 Visual Studio 2005 中编译 Win32 "hello world"的命令行
- Visual Studio C++ 2005/2010 with boost 1.50: warning C4267
- 错误 LNK 2005 和 LNK1169 C++ Visual Studio DLL
- 如何在 Windows for Visual Studio 2005 上设置 WXWIDGETS
- MSBuild、未解析的外部项和Visual Studio 2005
- 在Visual Studio 2005中自动更新版本号(C++/C#)
- LNK 2005链接Visual Studio 2010中函数的错误,但不链接类的错误
- Visual Studio 2005中的Lambda函数替换--Boost库
- STL 集自定义排序函数在 Qunicy 2005 中有效,但在 MS Studio 2010 中不起作用
- Visual Studio (VC++ 2005) 调试内存
- 在 Visual Studio 2005 中调试时显示环境变量
- visual studio 2005 C++ include path
- 从Visual Studio 2005移植到2008或更高版本
- Visual Studio 2005在我使用GSoap生成的soapclient.cpp中引发生成错误
- 如何使用visual studio 2005从命令行编译
- OCCI应用程序在Visual Studio 2005的调试模式下运行时会崩溃