首页 > 其他 > 详细

Copy Elision

时间:2021-05-19 13:05:19      阅读:11      评论:0      收藏:0      [点我收藏+]

Copy Elision

Mandatory(强制性) elision of copy/move operations

Under the following circumstances, the compilers are required to omit the copy and move construction of class objects, even if the copy/move constructor and the destructor have observable side-effects. The objects are constructed directly into the storage where they would otherwise be copied/moved to. The copy/move constructors need not be present or accessible:

  • In a return statement, when the operand is a prvalue of the same class type (ignoring cv-qualification) as the function return type:
      T f()
      {
        return T();
      }
      f();//only one call to default constructor of T
    
  • In the initialization of an object, when the initializer expression is a prvalue of the same class type (ignoring cv-qualification) as the variable type:
  T x = T(T(f()));

Non-mandatory(非强制的) elision of copy/move (since C++11) operations

When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the 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.126 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.This variant of copy elision is known as NRVO, "named return value optimization".

  • in a throw-expression, when the operand is the name of a non-volatile automatic object (other than a function or catch-clause parameter) whose scope does not extend beyond the end of the innermost enclosing try-block (if there is one), the copy/move operation from the operand to the exception object (15.1) can be omitted by constructing the automatic object directly into the exception object

  • when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move

  • when the exception-declaration of an exception handler (Clause 15) declares an object of the same type (except for cv-qualification) as the exception object (15.1), the copy/move operation can be omitted by treating the exception-declaration as an alias for the exception object if the meaning of the program will be unchanged except for the execution of constructors and destructors for the object declared by the exception-declaration.

注意:

copy elision is the only allowed form of optimization.因为有些编辑器在一些情况不应用copy elision(比如处于调试状态)。
实例:

#include <iostream>

struct Thing {
    Thing()  { std::cout << "default constructor" << std::endl; }
    ~Thing() { std::cout << "destructor"          << std::endl; }

    Thing(const Thing&)            { std::cout << "copy constructor\n";         }
    Thing& operator=(const Thing&) { std::cout << "copy assignment operator\n"; }
};

Thing f() {
     Thing t;
     return t;
}

int main() {
    Thing  t2 = f();
    return 0;
}

编译并运行,其结果如下:

$ g++ a.cpp -o a
$ ./a
default constructor
destructor

预期的那次 copy constructor 调用并没有出现。这里省略了两次调用copy constructor。可以这样看:局部变量t直接初始化了变量t2。添加了move构造函数是同样的效果。从临时对象到t2的的move构造被省略了。

Copy Elision

原文:https://www.cnblogs.com/ultramanX/p/14784053.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!