【发布时间】:2019-02-18 03:31:33
【问题描述】:
一个简单的程序如下,完全没有c++11语法(e.cpp)
#include<iostream>
using namespace std;
namespace m{
class my{
public:
enum A{
u=1,
v=2,
w=3
};
static A f(A a){
return (A)(a + A::u);
}
};
int main(){
using namespace m;
my::A r=my::f(my::u);
return 0;
}
使用g++4.1.2编译:
e.cpp:17:2: warning: no newline at end of file
e.cpp: In static member function ‘static m::my::A m::my::f(m::my::A)’:
e.cpp:11: error: expected primary-expression before ‘)’ token
e.cpp:11: error: ‘A’ is not a class or namespace
将 g++4.9.2 与 -std=c++98 一起使用
g++ e.cpp -std=c++98
e.cpp: In static member function ‘static m::my::A m::my::f(m::my::A)’:
e.cpp:11:36: error: ‘A’ is not a class or namespace
static A f(A a){return (A)(a + A::u);}
^
但是使用 -std=c++11 是可以的:
g++ e.cpp -std=c++11
为了使其使用 c++98 编译,我将其更改为避免 "A::" 为:
static A f(A a){return (A)(a + u);}
似乎在 c++98 下,嵌入的枚举类在类内部无法识别,而在 c++11 中它可以工作。这是枚举分辨率的差异,还是 c++98 标准中的一些以前的语法错误?
【问题讨论】:
-
C++98 中没有“枚举类”这样的东西。
标签: c++ class c++11 enums c++98