public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: James Stern <jsternitg@yahoo.com>
To: Jay Krell <jay.krell@cornell.edu>, swe sd <ccwork@hotmail.com>
Cc: cygwin@sourceware.cygnus.com
Subject: Re: problem in C++ pointer
Date: Thu, 16 Mar 2000 08:39:00 -0000	[thread overview]
Message-ID: <20000316163905.4649.qmail@web1406.mail.yahoo.com> (raw)

--- Jay Krell <jay.krell@cornell.edu> wrote:

> [... Is this undefined?]
> class X
> {
> public:
>     X* F(int i) { printf("%d", i); return this;}
> };
> 
> int i = 0;
> X x, *px = &x;
> px->F(i++)->F(i++)->F(i++);
> 
> is the expected output 012 or is it undefined?

Undefined.  The C++ Standard requires a sequence point before the
function is called but not before its argument are evaluated.  And
the "->" operators don't impose an evaluation order on their operands
either.

This has some interesting consequences.  Consider:

   cout << 'a' << flush;

The obvious implementation would be to make "flush" an object whose
constructor flushes the stream (although you'd have to pass the stream
as an argument then).

But that won't work because there's nothing to stop the implementation
from constructing "flush" before calling either operator<<.

So the iostream package uses a different solution.  It makes "flush" a
function pointer.  By itself, "flush" does nothing.  But an overloaded
operator<< accepts "flush" as an argument and *it* flushes the stream.

All this is necessary because the order of evaluation of operands above
is unspecified.  (Except that all operands will be evaluated before the
function is called.)  All that's really guaranteed is that the
operator<< calls will be evaluated left-to-right.

> [...] 
> [intro.execution]1.8.17 "..There is also a sequence point after the
> copying
> of a returned value and before the execution of any expressions
> outside the
> function". I don't know if that "copying of a returned value" applies
> to
> pointers and references, and I find "expressions outside the
> function"
> unclear.
> 
> Ordinarily, the sides of a -> I don't believe have a defined order of
> evaluation:

True.

> [...] 

> 
> "sequence point" is not in the index..

It is in my copy.  Maybe you're looking at a draft?  "point" is a
subtopic under "sequence."

=====
-- 
Opinions expressed above are not necessarily my employer's.
James M. Stern
ITG Inc.  Culver City, CA (213) 270-7955

__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com

             reply	other threads:[~2000-03-16  8:39 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-03-16  8:39 James Stern [this message]
  -- strict thread matches above, loose matches on Subject: below --
2000-03-17 13:26 Heribert Dahms
2000-03-17 13:15 Jay Krell
2000-03-17  9:48 Jay Krell
2000-03-13  2:07 Jay Krell
2000-03-13  1:54 Bernard Dautrevaux
2000-03-12 19:51 Jay Krell
2000-03-12  7:52 swe sd
2000-03-12  0:06 Jay Krell
2000-03-11 21:20 swe sd
2000-03-11 16:33 Jay Krell
2000-03-11 16:29 Jay Krell

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=20000316163905.4649.qmail@web1406.mail.yahoo.com \
    --to=jsternitg@yahoo.com \
    --cc=ccwork@hotmail.com \
    --cc=cygwin@sourceware.cygnus.com \
    --cc=jay.krell@cornell.edu \
    /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).