简体   繁体   English

为什么没有复制构造函数的NRVO不起作用

[英]Why doesn't NRVO work without copy constructor

I'm reading the book Inside C++ Object Model and I got things below: 我正在阅读《 Inside C ++ Object Model 》一书,发现以下内容:
在此处输入图片说明

As I know, NRVO won't call the copy constructor of the class that a function returns.So I don't understand why "This first verison of the program does not apply NRV optimization because of the absence of a copy constructor...". 据我所知,NRVO不会调用函数返回的类的副本构造函数。所以我不明白为什么“由于缺少副本构造函数,该程序的第一个版本没有应用NRV优化... ”。

The idea is that the code should work both for compilers that perform the optimization and for those that don't. 这个想法是,代码应该对执行优化的编译器和不执行优化的编译器都起作用。 If the object is copied, obviously the copy constructor must be defined and accessible. 如果对象被复制,显然必须定义复制构造函数并且可以访问它。 So that's the requirement, even for those compilers that might not always use it. 这就是要求,即使对于那些可能不总是使用它的编译器也是如此。

Like said in the comments, C++17 will make the optimizations mandatory and can then relax the requirements. 如评论中所述,C ++ 17将强制进行优化,然后可以放宽要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM