This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Incompatible behavior -O0, -O3, std::cout
Hello Everyone
I noticed some thing strange recently. This code (under g++ (GCC) 3.2.3
20030502 (Red Hat Linux 3.2.3-53)), provides this output with -O0 flag:
-f2() called
-f1() called-
12
And with -O3 flag:
-f1() called-
-f2() called
12
Here's the code:
#include <cstdio>
#include <iostream>
int f1() {
fprintf(stderr, "-f1() called-\n");
return 1;
}
int f2() {
fprintf(stderr, "-f2() called\n");
return 2;
}
int main(int argc, char **argv) {
std::cout << f1() << f2() << std::endl;
}
I'm pretty sure that I am depending on an undefined behavior here, but
maybe you guys would want to have a deeper look at this.
Also, if this is not very off-topic, could some one please tell me whether
the << operators for std::ostream are members of the class, or are global
functions (operators).
I think, if are global, it would be same as depending upon the order of
evaluation of arguments to a function (which would be wrong according to
the C++ standard), but if they were members of the stream classes, then it
would evaluate to a().b().c().d() and we should expect f1() to be called
before f2(). - Is that correct?
Thanks and Best Regards
Pankaj
--
--------------------------------------------------
Pankaj Gupta
Infrastructure Team - Tower Research Capital
Phone: 212-219-6012 [Work]
551-358-0684 [Cell]
Mail: pankaj.gupta@tower-research.com
--------------------------------------------------