public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* other/9693: sequence point consistency
@ 2003-02-13 17:06 lxg8906
0 siblings, 0 replies; only message in thread
From: lxg8906 @ 2003-02-13 17:06 UTC (permalink / raw)
To: gcc-gnats
>Number: 9693
>Category: other
>Synopsis: sequence point consistency
>Confidential: no
>Severity: non-critical
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Thu Feb 13 17:06:01 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator: Frank G
>Release: g++ 3.2
>Organization:
>Environment:
Solaris 2.8 Sparc9
>Description:
>How-To-Repeat:
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="bug_report.txt"
Content-Disposition: inline; filename="bug_report.txt"
Dear Sir/Madam,
The version I am using is g++ 3.2, platform is Solaris 2.8.
<program 1>
int main()
{
int i=2;
int j=(++i)+(++i);
cout<<j<<endl;
return 0;
}
The output is 7. It seems that (++i) is evaluated first, yields 3, increments, then 3+4=7. I am a little confused here. C++ std 5.1/4 reads: "Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expresion..." Should we have i modified twice here? Or this statement is just undefined? Solaris CC gives j=8 which I think is more reasonable.
Consider the following <program 2>
int& foo(int& i)
{
return ++i;
}
int main()
{
int i=2;
int j=foo(i)+foo(i);
cout<<j<<endl;
return 0;
}
foo() is just an in-place increment function, g++ gives j=8. Is it that
j=foo(i)+foo(i) is the same as j=(++i)+(++i) ? why they give two different results?
If I wrap the integer in a class, as follows,
<program 3>
#include <iostream>
using namespace std;
class T
{
public:
int m_b;
T(int i=0): m_b(i) {}
T& operator ++(void) { ++m_b; return *this;}
T operator +(cosnt T& rhs) { m_b+=rhs.m_b; return *this;}
void foo(void) {cout<<m_b<<endl;}
};
int main()
{
T t1(2);
T t2=(++t1)+(++t1);
t2.foo();
return 0;
}
The result is j=8.
In the program 2 and 3, if we change the return type in foo() and operator++ to T&, j=7.
Is it reasonble to have program 1-3 have different results when they in fact are doing the same thing?
Thank you.
Frank G.
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2003-02-13 17:06 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-13 17:06 other/9693: sequence point consistency lxg8906
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).