G++的命名返回值优化 -- NRVO
来源:互联网 发布:软件项目计划书实例 编辑:程序博客网 时间:2024/06/04 17:52
今天在运行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默认是不会开启该优化选项的。
- G++的命名返回值优化 -- NRVO
- 『C/C++』G++的命名返回值优化 -- NRVO
- clang++ 优化返回代码NRVO
- 命名返回值优化
- C++命名返回值优化
- Visual C++ 中的命名返回值优化
- Visual C++ 2005 中的命名返回值优化
- Visual C++ 2005 中的命名返回值优化
- Visual C++ 2005 中的命名返回值优化
- Visual C++ 2005中的命名返回值优化
- 命名返回值优化和成员初始化队列
- Visual C++ 2005 中的命名返回值优化
- 带命名空间的返回值类型
- C++的返回值优化
- 编译器的返回值优化
- C++ RVO/NRVO以及move语义的影响
- 查询优化器的命名
- 多线程下的返回值优化陷阱
- VS2010 hello world !
- 刚刚结束Autodesk 2013 DevDays 北京和上海的会议
- 用stage3d加速2d arpg游戏的性能测试和处理思路
- C# socket 网络编程
- jQuery实现CheckBox全选、全不选
- G++的命名返回值优化 -- NRVO
- 读取列表中的项的名称和类型,对应于数据库的每一列的字段的名称和类型
- c#使用 VTemplate模板引擎制作html报表
- 学的梦幻--六孔陶笛
- commons-lang
- linux环境变量
- 深入解析AsyncTask
- 反转一个句子
- 自定义TextView 可复制粘贴