This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
other/9693: sequence point consistency
- From: lxg8906 at yahoo dot com
- To: gcc-gnats at gcc dot gnu dot org
- Date: 13 Feb 2003 17:00:39 -0000
- Subject: other/9693: sequence point consistency
- Reply-to: lxg8906 at yahoo dot com
>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.