Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Find The Bug

Reply
Thread Tools

Find The Bug

 
 
David Rasmussen
Guest
Posts: n/a
 
      01-15-2004
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

 
Reply With Quote
 
 
 
 
Fao, Sean
Guest
Posts: n/a
 
      01-15-2004
> cout << p << " " << p++ << " " << ++p << endl;
> Is there a bug in the code, or in the compilers?


In the FAQ.
 
Reply With Quote
 
 
 
 
David Rasmussen
Guest
Posts: n/a
 
      01-15-2004
Fao, Sean wrote:
>> cout << p << " " << p++ << " " << ++p << endl;
>>Is there a bug in the code, or in the compilers?

>
>
> In the FAQ.


Thanks. I've looked, but I can't find anything relevant. Could be more
specific?

/David

 
Reply With Quote
 
Sumit Rajan
Guest
Posts: n/a
 
      01-15-2004

"David Rasmussen" <> wrote in message
news:bu6u3g$sv6$...
> 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



Comeau C++ came up with the expected output: 0 0 2


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-15-2004
Sumit Rajan wrote:
>
> > 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

>
> Comeau C++ came up with the expected output: 0 0 2


Doesn't matter what's expected. What matters is what's required. The
behavior of the program is undefined. Anything goes.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
Jonathan Turkanis
Guest
Posts: n/a
 
      01-15-2004
"David Rasmussen" <> wrote in message
news:bu6vfe$tb8$...
> Fao, Sean wrote:
> >> cout << p << " " << p++ << " " << ++p << endl;
> >>Is there a bug in the code, or in the compilers?

> >
> >
> > In the FAQ.

>
> Thanks. I've looked, but I can't find anything relevant. Could be

more
> specific?
>


I couldn't either. Try this:

http://www.langer.camelot.de/Article...ncePoints.html

Jonathan


 
Reply With Quote
 
David Rasmussen
Guest
Posts: n/a
 
      01-15-2004
osmium wrote:
> David Rasmussen writes:
>
>
>> cout << p << " " << p++ << " " << ++p << endl;

>
> Did you allow for the fact that shift left has a lower precedence than
> postfix ++ and prefix ++?
>


Even if, the first p should print out as 0. Shouldn't it?

/David

 
Reply With Quote
 
David Rasmussen
Guest
Posts: n/a
 
      01-15-2004
Pete Becker wrote:
>
> Doesn't matter what's expected. What matters is what's required. The
> behavior of the program is undefined. Anything goes.
>


I would sure like to know why...

/David

 
Reply With Quote
 
Fao, Sean
Guest
Posts: n/a
 
      01-15-2004
David Rasmussen wrote:

> Fao, Sean wrote:
>>> cout << p << " " << p++ << " " << ++p << endl;
>>>Is there a bug in the code, or in the compilers?

>>
>>
>> In the FAQ.

>
> Thanks. I've looked, but I can't find anything relevant. Could be more
> specific?


I thought I had seen it in the C++ FAQ at one time but I might just be
dreaming. Oh well, it's in the C FAQ for the same reason...

http://www.eskimo.com/~scs/C-faq/s3.html
 
Reply With Quote
 
David Rasmussen
Guest
Posts: n/a
 
      01-15-2004
Thanks!

I don't know what I was thinking. I know about the sequencing point type
problem. My mind was focused on the macros. I was sure I had some bug.

/David

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
*bug* *bug* *bug* David Raleigh Arnold Firefox 12 04-02-2007 03:13 AM
Found a Find.find() bug? robertlaferla@comcast.net Ruby 4 02-15-2007 10:28 PM
Find.find bug in 1.8.4? Jonathan Leighton Ruby 0 03-21-2006 06:44 PM
Find.find does not find orphaned links? Wybo Dekker Ruby 1 11-15-2005 02:50 PM
Find.find bug? Martin DeMello Ruby 5 09-13-2004 04:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57