473,473 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Find The Bug

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

Jul 22 '05 #1
25 2047
> cout << p << " " << p++ << " " << ++p << endl;
Is there a bug in the code, or in the compilers?


In the FAQ.
Jul 22 '05 #2
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

Jul 22 '05 #3

"David Rasmussen" <da*************@gmx.net> wrote in message
news:bu**********@news.net.uni-c.dk...
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
Jul 22 '05 #4
"David Rasmussen" <da*************@gmx.net> wrote in message
news:bu**********@news.net.uni-c.dk...
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
Jul 22 '05 #5
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)
Jul 22 '05 #6
David Rasmussen writes:
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


Did you allow for the fact that shift left has a lower precedence than
postfix ++ and prefix ++?
Jul 22 '05 #7
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

Jul 22 '05 #8
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

Jul 22 '05 #9
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
Jul 22 '05 #10
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

Jul 22 '05 #11
David Rasmussen wrote:
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.


Where is there a sequence point problem?

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #12

"David Rasmussen" <da*************@gmx.net> wrote in message news:bu**********@news.net.uni-c.dk...
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?

In the code. You have undefined behavior. You modify p multiple
times between sequence point for certain allowable orderings.

Jul 22 '05 #13

"Pete Becker" <pe********@acm.org> wrote in message news:40***************@acm.org...
Doesn't matter what's expected. What matters is what's required. The
behavior of the program is undefined. Anything goes.

Not undefined, it's just that his operators are invoked in an unspecified order.
Remember there are overloads for the operator++.
Jul 22 '05 #14

"David Rasmussen" <da*************@gmx.net> wrote in message news:bu**********@news.net.uni-c.dk...
=> cout << p << " " << p++ << " " << ++p << endl;
}


Argument evaluation order is not fixed in C++. The order of evaluating:
the three subexpressions "p", "p++" and "++p" is unspecified. Might happen
left to right, my happen right to left, might happen in any order. All you know
is that they happen before their respective << operators (but no guarantee
with respect to each other).
Jul 22 '05 #15

"Ron Natalie" <ro*@sensor.com> wrote in message news:40***********************@news.newshosting.co m...


In the code. You have undefined behavior. You modify p multiple
times between sequence point for certain allowable orderings.


BZZT. Caught myself here. Not undefined behavior as there are inherent
sequence points in the function calls. operator ++ is overloaded here.

Jul 22 '05 #16
David Rasmussen wrote:

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?


Nope. The behavior of the code is undefined, so there's nothing it
"should" do. I can't tell from your messages whether you've seen what
the problem is. Here's a simpler version:

cout << i++ << i++;

The code modifies the value of i twice without an intervening sequence
point.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #17

"Pete Becker" <pe********@acm.org> wrote in message news:40***************@acm.org...
cout << i++ << i++;

The code modifies the value of i twice without an intervening sequence
point.


No it does not. The ++ operators are overloads. There are sequence points
before and after the modification (the ones inherent in calling and returning from
functions).

His only sin is to expect that function arguments are to be evaluated in some particular
order.

Jul 22 '05 #18
"Ron Natalie" <ro*@sensor.com> wrote in message
news:40***********************@news.newshosting.co m...

"Pete Becker" <pe********@acm.org> wrote in message news:40***************@acm.org...
cout << i++ << i++;

The code modifies the value of i twice without an intervening sequence point.


No it does not. The ++ operators are overloads. There are

sequence points before and after the modification (the ones inherent in calling and returning from functions).

His only sin is to expect that function arguments are to be evaluated in some particular order.


I made the same mistake above.

Jonathan
Jul 22 '05 #19
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:bu************@ID-216073.news.uni-berlin.de...
"David Rasmussen" <da*************@gmx.net> wrote in message
news:bu**********@news.net.uni-c.dk...
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.


Neither could I. There must be a bug in the FAQ.

--
KCS
Jul 22 '05 #20
Ron Natalie wrote:

"Pete Becker" <pe********@acm.org> wrote in message news:40***************@acm.org...
Doesn't matter what's expected. What matters is what's required. The
behavior of the program is undefined. Anything goes.

Not undefined, it's just that his operators are invoked in an unspecified order.
Remember there are overloads for the operator++.


Where is the sequence point between the two increments?

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #21
Ron Natalie wrote:

"Ron Natalie" <ro*@sensor.com> wrote in message news:40***********************@news.newshosting.co m...


In the code. You have undefined behavior. You modify p multiple
times between sequence point for certain allowable orderings.


BZZT. Caught myself here. Not undefined behavior as there are inherent
sequence points in the function calls. operator ++ is overloaded here.


Ah, that's the point. Okay, unspecified here. Undefined for builtin
types. Bad business either way.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #22

"Pete Becker" <pe********@acm.org> wrote in message news:40***************@acm.org...
Ron Natalie wrote:

"Pete Becker" <pe********@acm.org> wrote in message news:40***************@acm.org...
Doesn't matter what's expected. What matters is what's required. The
behavior of the program is undefined. Anything goes.

Not undefined, it's just that his operators are invoked in an unspecified order.
Remember there are overloads for the operator++.


Where is the sequence point between the two increments?


They are done inside a SUBROUTINE. Operator++ is overloaded (these
are ENUMs). There is an sequence point after the value p is passed to the
function and when the value is returned.

Jul 22 '05 #23
Ron Natalie wrote:

They are done inside a SUBROUTINE.


A SUBROUTINE? This isn't FORTRAN. In C++ we have functions. <g>

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #24

"Pete Becker" <pe********@acm.org> wrote in message news:40***************@acm.org...
Ron Natalie wrote:

They are done inside a SUBROUTINE.


A SUBROUTINE? This isn't FORTRAN. In C++ we have functions. <g>

Hey, at least I didn't invoke it with GOSUB.

Jul 22 '05 #25
Kevin Goodsell wrote:

Where is there a sequence point problem?


There is none, as others have pointed out. I meant the undefined
evaluation order problem.

/David

Jul 22 '05 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dan Jones | last post by:
I'm writing a script to process a directory tree of images.  In each directory, I need to process each image and generate an HTML file listing all of the images and links to the subdirectories....
7
by: zhou | last post by:
Hi there, We have a compiler specific issue which requires us to force template instantiation. This works fine. The problem comes when I try using std:find() on vector. Since vector has no member...
0
by: amit | last post by:
I want to find out that if there is a mechanism to find a text inside a C# file and replace it with another string. I am using DTE to do it, the find proerty does it, the results are getting...
0
by: AMIT PUROHIT | last post by:
hi, this is a qry which I m stuck up with I want to find out that if there is a mechanism to find a text inside a C# file and replace it with another string. I am using DTE(EnvDTE) to do it,...
0
by: amit | last post by:
hi I have created a tool which does a find and replace thru DTE, now after it is done, it opens up a window, "FIND REACHED THE STARTING POINT OF SEARCH" I want to disbale this window...
5
by: Mike Labosh | last post by:
In VB 6, the Form_QueryUnload event had an UnloadMode parameter that let me find out *why* a form is unloading, and then conditionally cancel the event. In VB.NET, the Closing event passes a...
3
by: DJTN | last post by:
I'm getting the following error when I try to compile my setup project in VS 2002. I have re-installed the .net framework 1.1 and it didnt solve the problem. WARNING: Unable to find dependency...
3
by: David T. Ashley | last post by:
Hi, Red Hat Enterprise Linux 4.X. I'm writing command-line PHP scripts for the first time. I get the messages below. What do they mean? Are these operating system library modules, or...
0
by: Derek | last post by:
I am creating an intranet using Visual Web Developer Express Edition. Everything has been working OK until yesterday when I started getting 62 messages all beginning "Could not find schema...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.