问题描述
我有这个代码:
#include< iostream>
使用命名空间std;
#define DeclareEnumTricks(T)\
inline T& operator ++(T& e)\
{\
e = T(e + 1); \
返回e; \
} \
\
内联T运算符++(T& e,int)\
{\
T old = e; \
e = T(e + 1); \
返回旧; \
} \
\
inline T&运营商 - (T& e)\
{\
e = T(e-1); \
返回e; \
} \
\
内联T运算符 - (T& e,int)\
{\
T old = e; \
e = T(e-1); \
返回旧; \
} \
\
内联T end(T)\
{\
返回T ## _ end; \
} \
\
内联T开始(T)\
{\
返回T ## _ begin; \
}
enum Piece {none,pawn,knight,bishop,rook,queen,king,
Piece_begin = pawn,Piece_end = king + 1};
DeclareEnumTricks(Piece)
int main()
{
Piece p = none;
cout<< p
}
g ++和其他几个编译器的输出是_not_
0 0 2
如预期。在g ++中它是
2 1 1
代码或编译器中是否有错误?
/ David
在FAQ中。
在FAQ中。
$ b $谢谢。我看了,但我找不到任何相关的东西。可以更多
具体吗?
/ David
Comeau C ++提出了预期的输出:0 0 2
I have this code:
#include <iostream>
using namespace std;
#define DeclareEnumTricks(T) \
inline T& operator++(T& e) \
{ \
e = T(e+1); \
return e; \
} \
\
inline T operator++(T& e, int) \
{ \
T old = e; \
e = T(e+1); \
return old;\
} \
\
inline T& operator--(T& e) \
{ \
e = T(e-1); \
return e; \
} \
\
inline T operator--(T& e, int) \
{ \
T old = e; \
e = T(e-1); \
return old; \
} \
\
inline T end(T) \
{ \
return T##_end; \
} \
\
inline T begin(T) \
{ \
return T##_begin; \
}
enum Piece {none,pawn,knight,bishop,rook,queen,king,
Piece_begin = pawn, Piece_end = king + 1};
DeclareEnumTricks(Piece)
int main()
{
Piece p = none;
cout << p << " " << p++ << " " << ++p << endl;
}
The output from g++ and several other compilers is _not_
0 0 2
as expected. In g++ it is
2 1 1
Is there a bug in the code, or in the compilers?
/David
In the FAQ.
In the FAQ.
Thanks. I''ve looked, but I can''t find anything relevant. Could be more
specific?
/David
Comeau C++ came up with the expected output: 0 0 2
这篇关于找到Bug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!