Understanding eliding rules with regard to c++11

前端 未结 3 867
情书的邮戳
情书的邮戳 2020-12-06 14:17

I have been testing with rvalue references and move semantics and want to make sure I understand when a copy should be elided and when it should follow move semantics.

3条回答
  •  抹茶落季
    2020-12-06 14:55

    Quoting the C++11 standard (12.8/31), "When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the copy/move constructor and/or destructor for the object have side effects.", so:

    1. Copy elision is not guaranteed. (technically; but compiler vendors are motivated to so for marketing reasons)
    2. This is a compiler-specific question.
    3. Technically there are some cases (see here) but they would indicate (IMO) some error in the design.
    4. You should still provide move constructor (if it is possible to implement it effectively). There are some cases where copy elision is prohibited but move constructor is just fine:

      vector reverse(vector vec)
      {
          reverse( vec.begin(), vec.end() );
          return vec;
      }
      
      auto vec = reverse(vector{"abc", "def", "ghi"});
      

    Copy elision is explicitly not allowed to propagate from function argument to the final automatic value initialized from a temporary. But no copy is called due to move constructor.


    To be more specific about my last remark, I quote N3290, which is difficult to get nowadays, but it is very close to N3337:

    12.8/31:

    When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the copy/move constructor and/or destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move 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. This elision of copy/move operations, called copy elision, 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 (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value

    12.8/32:

    When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue. If overload resolution fails, or if the type of the first parameter of the selected constructor is not an rvalue reference to the object’s type (possibly cv-qualified), overload resolution is performed again, considering the object as an lvalue. [...]

提交回复
热议问题