Re: Function call evaluation order
- From: "Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx>
- Date: Fri, 24 Mar 2006 07:09:50 -0800
Frederico Pissarra wrote:
I agree that the code used as example is a bit confusing - after all,
i is incremented after each function call or after all funcions?. But
this is not "undefined" behavior!
In every compiler I tested this code (GCC, Borland C++, Visual C++ 6,
7 and 8 and Watcom C++) the result is the same... Because structure
member operator, function calls and array offset operator have higher
precedence than pos or pre increment, those are made AFTER the entire
sentence... this is the expected behavior in C language!
Not so. You're guaranteed that all of the evaluation of the parameters of a
function call complete before the function call begins, but when you have a
"full expression" (everything up to the semicolon) that contains multiple
function calls, there are no guarantees at all about the order of evaluation
of the parameters of a given function nor the order in which functions will
be called (except as necessary to obey precedence rules).
Are you sure you tried this with VC8? The following program:
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char* data[] = { "one ", "two ", "three " };
int i = 0;
string s;
s.assign(data[i++]).append(data[i++]).append(data[i++]);
cout << s << endl;
i = 0;
s.assign(data[i++]);
s.append(data[i++]);
s.append(data[i++]);
cout << s << endl;
}
compiled with VC8 with out without optimization produces
one one one
one two three
as it's output. If precedence alone dictated behavior, the two lines of
output would be the same. That is undefined behavior.
-cd
.
- Follow-Ups:
- Re: Function call evaluation order
- From: Carl Daniel [VC++ MVP]
- Re: Function call evaluation order
- References:
- Function call evaluation order
- From: Cheng
- Re: Function call evaluation order
- From: Frederico Pissarra
- Re: Function call evaluation order
- From: Carl Daniel [VC++ MVP]
- Re: Function call evaluation order
- From: Frederico Pissarra
- Re: Function call evaluation order
- From: Carl Daniel [VC++ MVP]
- Re: Function call evaluation order
- From: Frederico Pissarra
- Function call evaluation order
- Prev by Date: Re: error LNK2001: unresolved external symbol __beginthread
- Next by Date: Re: not a warning?
- Previous by thread: Re: Function call evaluation order
- Next by thread: Re: Function call evaluation order
- Index(es):
Relevant Pages
|
Loading