Topic: which of these is standard comforming or neither


Author: "Matt Seitz" <mseitz@meridian-data.com>
Date: 1998/12/04
Raw View
James Kuyper wrote in message <3666DD4B.446B@wizard.net>...

>ericleif wrote:
>> So is there any way to explicitly tell the compiler which order to
>> process it, without storing the return in some other variable?
>
>Insert some sequence points:
>
> cout << "There are ";
> cout << box_in_box(foo, bar);
> cout << "boxes here." << endl;


Could one also force the left-to-right ordering by turning box_in_box into a
functor (like endl) instead of a function?




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 1998/12/03
Raw View
ericleif wrote:

> In article <7441l9$194$1@client2.news.psi.net>, seth@kansmen.com says...
>  > Steve Clamage wrote in message <741t2u$25p$1@engnews2.Eng.Sun.COM>...
>  > >REMOVE_ericleif@mindspring.com (ericleif) writes:
>  > >>Testing this on two compilers I got 2 different outputs, I am wondering
>  > >>which is doin it correctly.
>  > >>cout << "There are " << box_in_box(foo, bar) << " boxes here." << endl;
>
...
>  > different). The
>  > real reason is that C++ does not define the order of evaluation of
>  > subexpressions. In the expression
>  >
>  > (cout << "There are ") << box_in_box( foo, bar )
>  >
>  > which is part of the statement given above, with parentheses added to
>  > clarify how this will get
>  > parsed, a compiler is free to execute (cout << "There are ") and
>  > ox_in_box( foo, bar ) in any order.

> So is there any way to explicitly tell the compiler which order to
> process it, without storing the return in some other variable?

Insert some sequence points:

 cout << "There are ";
 cout << box_in_box(foo, bar);
 cout << "boxes here." << endl;



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: scott douglass <sdouglass%_%junk@_.arm.com>
Date: 1998/12/02
Raw View
Steve Clamage wrote:
>
> REMOVE_ericleif@mindspring.com (ericleif) writes:
>
> >Testing this on two compilers I got 2 different outputs, I am wondering
> >which is doin it correctly.
>
> >cout << "There are " << box_in_box(foo, bar) << " boxes here." << endl;
>
> >The box_in_box function is a recursive function which shouldn't really
> >matter right, but anyway one compiler, waits until the return from the
> >box_in_box function before outputing the line and the other outputs
> >"There are " and then proceeds into the function, after the function
> >returns the rest of the statement is output.
>
> Both compilers are correct.
>
> It is up to the implementation whether data written to cout is
> flushed after each operation, at the end of a line, or at some
> other time. By default cout must be flushed each time cin is read,
> and of course as the result of an explicit "flush" call (such as
> via the endl or flush manipulators).

I agree that "Both compilers are correct.", but the reason is a lack of sequence
points not flushing.  I'm assuming the output from box_in_box is written to the
same stream, i.e. cout.  No matter when the flushing happens the interleaving on
the same stream would be the same.

The question is about how things that box_in_box writes to cout will be
interleaved with respect to the writes to cout in the rest of the expression.

Consider just the subexpression:
    cout << "There are " << box_in_box(foo, bar)

This expression means:
    ((cout.operator<<("There are ")).operator<<(box_in_box(foo, bar)))

In section 5.2.2 the standard says:
   The order of evaluation of the postfix expression and the argument expression
list is unspecified.

In this case the argument expression list is 'box_in_box(foo, bar)' and the
postfix expression is '(cout.operator<<("There are ")).operator<<'.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Seth Jones" <seth@kansmen.com>
Date: 1998/12/02
Raw View
Steve Clamage wrote in message <741t2u$25p$1@engnews2.Eng.Sun.COM>...

>REMOVE_ericleif@mindspring.com (ericleif) writes:

>>Testing this on two compilers I got 2 different outputs, I am wondering
>>which is doin it correctly.

>>cout << "There are " << box_in_box(foo, bar) << " boxes here." << endl;

>>The box_in_box function is a recursive function which shouldn't really
>>matter right, but anyway one compiler, waits until the return from the
>>box_in_box function before outputing the line and the other outputs
>>"There are " and then proceeds into the function, after the function
>>returns the rest of the statement is output.

>Both compilers are correct.

>It is up to the implementation whether data written to cout is
>flushed after each operation, at the end of a line, or at some
>other time. By default cout must be flushed each time cin is read,
>and of course as the result of an explicit "flush" call (such as
>via the endl or flush manipulators).


Right answer, wrong reason. If the only difference was when the output was
flushed, the
output would be the same (but the time it appeared on the screen would be
different). The
real reason is that C++ does not define the order of evaluation of
subexpressions. In the expression

(cout << "There are ") << box_in_box( foo, bar )

which is part of the statement given above, with parentheses added to
clarify how this will get
parsed, a compiler is free to execute (cout << "There are ") and
ox_in_box( foo, bar ) in any order.

Seth Jones




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/12/01
Raw View
REMOVE_ericleif@mindspring.com (ericleif) writes:

>Testing this on two compilers I got 2 different outputs, I am wondering
>which is doin it correctly.

>cout << "There are " << box_in_box(foo, bar) << " boxes here." << endl;

>The box_in_box function is a recursive function which shouldn't really
>matter right, but anyway one compiler, waits until the return from the
>box_in_box function before outputing the line and the other outputs
>"There are " and then proceeds into the function, after the function
>returns the rest of the statement is output.

Both compilers are correct.

It is up to the implementation whether data written to cout is
flushed after each operation, at the end of a line, or at some
other time. By default cout must be flushed each time cin is read,
and of course as the result of an explicit "flush" call (such as
via the endl or flush manipulators).

--
Steve Clamage, stephen.clamage@sun.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]