Search code examples
c++copy-constructornrvorvo

Is the object copied or not when RVO/NRVO kicks in?


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?


Solution

  • Officially, no, RVO/NRVO do not affect whether a program is well formed or not. The standard explicitly allows any side effects from the copy constructor to be omitted, but access checking on the copy constructor is still supposed to be done.

    Most compilers, however, will no do access checking on omitted copy constructors unless you ask for the rules to be enforced as strictly as possible. I'm not absolutely sure, but I seem to recall having used a few that skipped the access check even when you did ask for strict compliance (but I don't remember for sure).

    Edit (to correct what I see as a misconception expressed in some of the other answers):

    The paragraphs in a section of the standard are intended to be read in order. The requirements in the first paragraph that apply to a situation always apply. In this case, the permission to omit side effects is in paragraph 15. Immediately before that in paragraph 14, however, we see that:

    A program is ill-formed if the copy constructor or the copy assignment operator for an object is implicitly used and the special member function is not accessible (clause 11).

    So, we only get to paragraph 15 (where copying can be omitted) if the access check specified in paragraph 14 has already been passed.