#include <iostream> #include <string> #include <vector> using namespace std; struct Exmpl{ Exmpl() { cout << "Exmpl()" << endl; } Exmpl(const Exmpl&) { cout << "Exmpl(const Exmpl&)" << endl; } Exmpl& operator=(const Exmpl& rhs) { cout << "operator=Exmpl()" << endl; return *this; } ~Exmpl() { cout << "~Exmpl()" << endl; } }; void func1(Exmpl obj) { } void func2(Exmpl &obj) { } Exmpl func3() { Exmpl obj; return obj; } int main() { Exmpl eobj; func1(eobj); func2(eobj); eobj = func3(); Exmpl *p = new Exmpl; vector<Exmpl> evec(3); delete p; return 0; }
when compiling in g ++ (4.4.3) I got
Exmpl() Exmpl(const Exmpl&) ~Exmpl() Exmpl() operator=(const Exmpl&) ~Exmpl() Exmpl() Exmpl() Exmpl(const Exmpl&) Exmpl(const Exmpl&) Exmpl(const Exmpl&) ~Exmpl() ~Exmpl() ~Exmpl() ~Exmpl() ~Exmpl() ~Exmpl()
and in vs2008 the result:
Exmpl() Exmpl(const Exmpl&) ~Exmpl() Exmpl() Exmpl(const Exmpl&) ~Exmpl() operator=(const Exmpl&) ~Exmpl() Exmpl() Exmpl() Exmpl(const Exmpl&) Exmpl(const Exmpl&) Exmpl(const Exmpl&) ~Exmpl() ~Exmpl() ~Exmpl() ~Exmpl() ~Exmpl() ~Exmpl()
when the code goes to "eobj = func3 ();" basically, the 5th and 6th lines in vs2008 results cannot be found in g ++. I tried to perform several levels of optimization, but the result is the same. what is the reason for the difference?
source share