今天在运行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默认是不会开启该优化选项的。