I can't get my head around RVO (and NRVO) definition because of multiple questions like this one that to me look assuming that RVO omits a copy constructor. Now according to 12.8.15
In such cases, the implementation treats the source and target of the omitted copy operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.
Which looks like it's not the copy constructor call is being omitted, but the copy itself - just the object is constructed at the "copy" location in the first place and so there's no "original" object and no copying at all. So even when a class has a private
copy constructor it can be returned from a function when RVO kicks in because there's no copy.
Do I get it right? Is the copying itself omitted or is the copy constructor invokation omitted? Should returning an object from a function be allowed when the object class has a private copy constructor?
The copying is omitted if the optimization takes effect, but the compiler is still required to check that the copy constructor is accessible. Otherwise the code would be invalid in case the compiler (or some other compiler) decided not to optimize.
Is the copying itself omitted or is the copy constructor invokation omitted?
The copying operation itself is ommitted. If you have a look at the full quote it clearly mentions so:
C++ 03 12.8 copying class objects
Para 15
When certain criteria are met, an implementation is allowed to omit the copy construction of a class object, even if the copy constructor and/or destructor for the object have side effects. In such cases, the implemen-tation treats the source and target of the omitted copy operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.111)This elision of copy operations is permitted in the following circumstances (which may be combined to eliminate multiple copies):
— in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the function return type, the copy operation can be omitted by constructing the automatic object directly into the function’s return value.
— when a temporary class object that has not been bound to a reference (12.2) would be copied to a class object with the same cv-unqualified type, the copy operation can be omitted by constructing the temporary object directly into the target of the omitted copy.....
Should returning an object from a function be allowed when the object class has a private copy constructor?
RVO and NRVO are compiler optimizations, allowed by the compiler but not guaranteed What happens if a particular dumb compiler cannot provide these optimizations?
Without a accessible copy constructor the code will break on all such compilers which is not desired. Given that the copy constructor should be accessible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With