Re: Function call evaluation order
- From: "Doug Harrison [MVP]" <dsh@xxxxxxxx>
- Date: Sat, 25 Mar 2006 11:05:00 -0600
On Fri, 24 Mar 2006 20:57:33 -0800, Tim Roberts <timr@xxxxxxxxx> wrote:
"Igor Tandetnik" <itandetnik@xxxxxxxx> wrote:
"Tim Roberts" <timr@xxxxxxxxx> wrote:
"Igor Tandetnik" <itandetnik@xxxxxxxx> wrote:
"Cheng" <chengwuchew@xxxxxxxxxxx> wrote:
s.assign(data[i++]).append(data[i++]).append(data[i++]);
Not only would it be better - you must do so. The statement shown
above exhibits undefined behavior. There exists a possible legal
order of execution of various subexpressions that results in the
variable 'i' being modified more than once between sequence points.
Is it? I mean, I shudder in horror at that line of code, but I
thought a function call represented a sequence point. Not so?
A function call itself introduces a sequence point. But before the call
can be made, parameters need to be evaluated. They can be evaluated in
any order, and there is no sequence point between evaluation of two
parameters.
Further, in class member invocation of the form expr.method(params),
expr and params are evaluated in arbitrary order, again with no sequence
point.
Thus, in the example above, all three (i++) subexpressions may be
evaluated first with no intervening sequence points, before any calls
are made that may introduce said sequence points.
OK, I think I see the subtlety that I was missing.
what you're saying is that this:
s.assign(data[i++]).append(data[i++]).append(data[i++]);
is compiled as if it had been written:
string::append(
string::append(
string::assign( s, data[i++] ), data[i++], data[i++] ) );
So, even though it looks like three independent functions calls, it really
is three nested function calls.
That, I can believe.
For a continuation of this sort of analysis, see:
http://groups.google.com/group/microsoft.public.vc.language/msg/a5d3b3f8a4e3797c
--
Doug Harrison
Visual C++ MVP
.
- References:
- Function call evaluation order
- From: Cheng
- Re: Function call evaluation order
- From: Igor Tandetnik
- Re: Function call evaluation order
- From: Tim Roberts
- Re: Function call evaluation order
- From: Igor Tandetnik
- Re: Function call evaluation order
- From: Tim Roberts
- Function call evaluation order
- Prev by Date: Re: Abstract "POSITION"
- Next by Date: Re: Show DLL that is loading?
- Previous by thread: Re: Function call evaluation order
- Next by thread: Re: Function call evaluation order
- Index(es):
Relevant Pages
|