public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: lxg8906@yahoo.com
To: gcc-gnats@gcc.gnu.org
Subject: other/9693: sequence point consistency
Date: Thu, 13 Feb 2003 17:06:00 -0000	[thread overview]
Message-ID: <20030213170039.27334.qmail@sources.redhat.com> (raw)


>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.


                 reply	other threads:[~2003-02-13 17:06 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030213170039.27334.qmail@sources.redhat.com \
    --to=lxg8906@yahoo.com \
    --cc=gcc-gnats@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).