#include <iostream>
using namespace std;
struct Foo
{
Foo() { cout << "Foo ctor" << endl; }
Foo(const Foo&) { cout << "Foo copy ctor" << endl; }
void operator=(const Foo&) { cout << "Foo operator=" << endl; }
~Foo() { cout << "Foo dtor" << endl; }
};
//gcc 默認會進行rov優化,不會調用拷貝構造函數,直接把棧內的f對象提升級調用函數可用對象,避免調用拷貝構造函數及銷燬棧內的f對象的效率降低操作
Foo make_foo() //在VC debug默認
{
Foo f;
return f;
//return Foo();
}
int main(void)
{
make_foo();
return 0;
}
VC6.0 debug版本運行結果
Foo ctor
Foo copy ctor
Foo dtor
Foo dtor
GCC編譯器,默認是開戶rov優化的,其運行結果如下,沒有發生拷貝構造函數
Foo ctor
Foo dtor