复制省略

复制省略或者译作“省略不必要的复制”(copy elision),是C++语言标准中定义的编译优化技术。

当一个class类型temporary object的临时对象用于初始化同类型的对象[1]时,复制初始化通常优化为直接初始化;但在语义上仍然需要复制构造函数是可访问的。[2] 例如:

#include <iostream>

struct C {
  explicit C(int) {}
  C(const C&) { std::cout<<"copy constructor"<<std::endl; } // the copy constructor has a visible side effect
};                     // it modifies an object with static storage duration

int main() {
  C c1(42); // direct-initialization, calls C::C(42)
  C c2 = C(42); // copy-initialization elision as direct-initialization, so as to call C::C(42)
  
  return 0;
}

上例,在g++与Visual C++默认都是复制省略。

throw语句抛出一个异常对象,catch语句匹配一个异常对象,默认是要执行复制构造函数的。如下例:

#include <iostream>

struct C {
  C() {}
  C(const C&) { std::cout << "Hello World!\n"; }
};

void f() {
  C c;
  throw c; // copying the named object c into the exception object.
}          // 由于异常对象保存在线程信息块TIB中,这里的复制构造不可省略. 这使得调用栈上压栈保存的函数都有机会处理这个异常对象

int main() {
  try {
    f();
  }
  catch(C c) {  // copying the exception object into the temporary in the exception declaration.
  }             // 由于这里是传值的catch批配,因此复制初始化不可省略. 通常用于catch块可能会修改异常对象的内容,但又需要把原来的异常对象重新抛出(re-throw)
}

上例默认应该打印输出两次"Hello World!"

GCC的编译选项-fno-elide-constructors关闭复制省略。

参考文献

  1. ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §12.8 Copying class objects [class.copy] para. 15
  2. Sutter, Herb. . Addison-Wesley. 2001.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.