Re: Function call evaluation order
- From: "Frederico Pissarra" <frederico@xxxxxxxxxxx>
- Date: Fri, 24 Mar 2006 14:22:51 -0300
"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx>
escreveu na mensagem news:O1z0A71TGHA.4740@xxxxxxxxxxxxxxxxxxxxxxx
Here's an even better one:
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
template <class T>
T inc(T& i)
{
T t = i;
++i;
return t;
}
void f1()
{
char* data[] = { "one ", "two ", "three " };
int i = 0;
string s;
s.assign(data[i++]).append(data[i++]).append(data[i++]);
cout << s << endl;
}
void f2()
{
char* data[] = { "one ", "two ", "three " };
int i = 0;
string s;
s.assign(data[inc(i)]).append(data[inc(i)]).append(data[inc(i)]);
cout << s << endl;
}
void f3()
{
char* data[] = { "one ", "two ", "three " };
int i = 0;
string s;
s.assign(data[i++]);
s.append(data[i++]);
s.append(data[i++]);
cout << s << endl;
}
int main()
{
f1();
f2();
f3();
}
compiled with or without optimization, with VC8 or VC7.1 outputs
one one one
three two one
one two three
If precendence alone were enough, all three lines would be the same.
Undefined behavior in action! Only f3 has well-defined behavior - it must
produce "one two three " according to the C++ standard. f2 has partially
defined behavior as the only valid outputs from it consist of the strings
"one ", "two " and "three " once each in some undefined order. f1
has.completely undefined behavior since i is modified twice between
sequence points.
-cd
The behavior described can be explained easily with operator precedence:
Note that [] has higher precedence than () - funcion call. So 3
post-increments will be performed AFTER the whole expression is evaluated.
That's why "one one one" is the result... Is almost like if f1() is the same
thing as f3().
In f2() the template function inc() will calculate the values and put them
on stack (anonymous object)... since [] has higher precedence! That's why
the inverse order occurs...
Looking another way: [] has higher precedence than (), but inside the data[]
we have funcion calls that must be evaluated... so, all "inc(i)" are
evaluated before anything...
f3() uses simple calls...
The same to the function calls... they are always called in order from left
to right because "." has higher precedence then [] or ().
In this particular example there is no undefined behavior!
[]s
Fred
.
- 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
- Re: Function call evaluation order
- From: Carl Daniel [VC++ MVP]
- Re: Function call evaluation order
- From: Carl Daniel [VC++ MVP]
- Function call evaluation order
- Prev by Date: Re: Show DLL that is loading?
- Next by Date: Re: Function call evaluation order
- Previous by thread: Re: Function call evaluation order
- Next by thread: Re: Function call evaluation order
- Index(es):
Relevant Pages
|