On 08/12/2016 07:43, topher wrote: > #include <string> > #include <cstdio> > #include <sstream> > > using namespace std; > > struct StaticName { > string getName() { > static int i = 0; > name.clear(); > ostringstream oss; > oss << ++i; > name += oss.str(); > return name; > } > static string name; > }; > string StaticName::name = ""; > > int main(int argc, char* argv[]) { > StaticName sn; > printf("name1=%s, name2=%s, name3=%s\n", > sn.getName().c_str(), sn.getName().c_str(), sn.getName().c_str()); > return 0; > } > > The executable built by g++ 4.8.5 prints "name1=3, name2=2, name3=1". > However, executable built by clang++ 3.8.0 prints "name1=1, name2=2, > name3=3", which is what I expected. > Is there any undefined behavior in my code? I can't answer for C++ but in C99: 6.5.2.2 Function calls > 10) The order of evaluation of the function designator, the actual > arguments, and subexpressions within the actual arguments is > unspecified, but there is a sequence point before the actual call. So basically, foo(f1(), f2(), f3()); might evaluate in any order: f1, f2, f3 f1, f3, f2 f2, f1, f3 f2, f3, f1 f3, f1, f2 f3, f2, f1 I suppose the same rule applies to C++ And note that unspecified behavior is slightly different than undefined behavior. > 1 unspecified behavior > use of an unspecified value, or other behavior where this > International Standard provides two or more possibilities and imposes > no further requirements on which is chosen in any instance > 2 EXAMPLE > An example of unspecified behavior is the order in which the > arguments to a function are evaluated. Regards.