want.to.be.professer
8/29/2008 3:27:00 AM
On 8?29?, ??9?44?, Sam <s...@email-scan.com> wrote:
> want.to.be.professer writes:
> > Yesterday, my friend ask me a question about iostream.I know the
> > result ,but can't explain.The program is below:
>
> > ---------------------------------------------------------------------------------------------------------------------------------------------------
> > #include <iostream>
>
> > using namespace std;
>
> > int fun( int x )
> > {
> > cout << "____in fun()____" << endl;
> > return x;
> > }
>
> > int main()
> > {
> > int x = 9;
> > cout << "b = " << x << fun2( 2 ) << "ads" << endl;
> > return 0;
> > }
>
> > -------------------------------------------------------------------------------------------------------------------------------------------------------
> > The program run as below:
>
> > 1.function "fun()"
> > 2.operator << ( basic_iostream<char, _Traits>& _Ostr, const char*
> > _Val )
> > ( _Val== "b = " )
> > 3. a member function : operator<<(int _Val)
> > ( _Val == 9 )
> > 4. a member function : operator << ( int _Val )
> > ( _Val == 2 )
> > 5. operator << ( basic_iostream<char, _Traits>& _Ostr, const char*
> > _Val )
> > ( _Val== "ads" )
>
> > But why it run like this? I can't explain.So I put in here to wait a
> > answer.
>
> Because the C++ standard does not specify the evaluation order for
> individual parts of an expression. A C++ compiler is free to choose any
> evaluation order. Consider a simpler example:
>
> a= b() + c();
>
> The C++ standard does not specify whether the b() function should be called
> before c(), or if c() should be called first, then b(), then the results be
> added. A compiler is free to generate code that calls the two functions in
> any order, before adding the results, and storing the result of the addition
> in a. The C++ standard only defines "sequence points", where all evaluations
> must be complete. In the above example, the semicolon is the sequence point.
> This is somewhat of an oversimplification, but it gives you the general
> idea.
>
> In your example:
>
> cout << "b = " << x << fun2( 2 ) << "ads" << endl;
>
> The function call to fun2() may be executed at any time before the code that
> corresponds to " << "ads" ", as long as the return value from the function
> call is sent to the stream in the right "spot".
>
> application_pgp-signature_part
> < 1KViewDownload
Thanks.Now I know "sequence points", and I saw a paragraph from other
web site,
which says that C++ group the "sequence points" into 5 situation:
At the end of a full expression
After the evaluation of all function arguments in a function call and
before execution of any expressions in the function body
After copying of a returned value and before execution of any
expressions outside the function
After evaluation of the first expression in a&&b, a||b, a?b:c, or
a,b
After the initialization of each base and member in the constructor
initialization list
Who has the Stardard C++ Doc about "sequence points" ?