Today I wrote a wrapper class to do database queries. My idea was to inherit from std::stringstream to be able to write something like this:
Statement st;
st << "SELECT name FROM user WHERE email LIKE " << st.prep("john") << ";";
st.exec();
While testing I realized that the execution order of the prep function differs depending on the compiler used. Look at this simple snippet:
#include <sstream>
#include <iostream>
unsigned nr = 0;
std::string prep() {
std::stringstream s;
s << nr++;
return s.str();
}
int main(int argc, char** argv) {
std::cout << prep() << " " << prep() << " " << prep() << "\n";
return 0;
}
You could think that output will be "0 1 2" and yes, if compiled with icc or clang++ it behaves like that. But when compiling with g++ the output for me is "2 1 0".
Is there any way to ensure ascending execution order?