One reason is that the order of initialization is not defined. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
externint a,b;
//EDIT:
int f(constchar *,int);
//~EDIT
int a=f("a",b);
int b=f("b",a);
int f(constchar *s,int i){
std::cout <<s<<": "<<i<<std::endl;
return 0;
}
int main(){}
If you try running that, you'll see that the output is either
a: <garbage>
b: 0
or
b: <garbage>
a: 0
But it's not possible to know ahead of time which one.
In fact, just using std::cout in that function is unsafe.
now I tried putting the function implementation of f to after main, and the assignments of a & b to inside main but I still get error: undefined reference to _a
but I cannot recreate the error as described by helios.
My point is, at least as long as I keep using codeblocks and mingw I'm not going to bother editing my functions called b4 main because my compiler at least seems to handle those situations nicely.
well all i'm saying is undefined behaviour is undefined behaviour for c++ ingeneral, what is undefined behaviour for c++ in general might be defined for my particular compiler.
You can't know that by just observing its behavior under particular circumstances. You'd have to look at the manual.
By the way, GCC is known to have no qualms about breaking semantics between versions when they're supposed to be undefined anyway. Have fun.