问题
I'm reading the book Inside C++ Object Model and I got things below:
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...".
回答1:
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.
来源:https://stackoverflow.com/questions/42645939/why-doesnt-nrvo-work-without-copy-constructor