今天在運行C++ primer中一段程序的時候,沒有打印複製構造函數的信息,代碼如下:
class Exmpl {
public:
Exmpl() {cout << "Exmple()" << endl;}
Exmpl(const Exmpl&) {cout << "Exmple(const Exmpl&)" << endl;}
Exmpl & operator = (const Exmpl &rhs)
{
cout << "operator = (const Exmple&rhs)" << endl;
return *this;
}
~Exmpl()
{
cout << "~Exmpl()" << endl;
}
};
void func1(Exmpl obj)
{
}
void func2(Exmpl &obj)
{
}
Exmpl func3()
{
Exmpl obj;
return obj;
}
int main(int argc, char **argv)
{
Exmpl eobj;
func1(eobj);
func2(eobj);
eobj = func3();
}
按照我的分析,輸出結果如下:
Exmple()
Exmple(const Exmpl&)
~Exmpl()
Exmple()
Exmple(const Exmpl&)
~Exmpl()
operator = (const Exmple&rhs)
~Exmpl()
~Exmpl()
可是我電腦上的輸出如下(我使用的是g++ 4.0.2)
Exmple()
Exmple(const Exmpl&)
~Exmpl()
Exmple()
operator = (const Exmple&rhs)
~Exmpl()
~Exmpl()
查閱資料,原來是g++默認啓用了命名返回值優化,named return value optimizatio - NRVO
加上-fno-elide-constructors後,就可以關掉NRVO了。
g++的手冊說:
-fno-elide-constructors
The C++ standard allows an implementation to omit creating a
temporary which is only used to initialize another object of the
same type. Specifying this option disables that optimization, and
forces G++ to call the copy constructor in all cases.
我在Visual Studio 2005下運行上面的程序,輸出和沒有加-fno-elide-constructors的g++編譯出來的程序一樣。這說明Visual Studio 2005默認是不會開啓該優化選項的。