Copy elision

Copy elision

In C++ computer programming, copy elision refers to a compiler optimization technique that eliminates unnecessary copying of objects. The C++ language standard generally allows implementations to perform any optimization, as long as the resulting program's observable behavior is the same as if the program was executed exactly as mandated by the standard.

The standard also describes a few situations where copying can be eliminated even if this would alter the program's behavior, the most common being the return value optimization. Another widely implemented optimization, described in the C++ standard, is when a temporary object of class type is copied to an object of the same type.[1] As a result, copy-initialization is usually equivalent to direct-initialization in terms of performance, but not in semantics; copy-initialization still requires an accessible copy constructor.[2] The optimization can not be applied to a temporary object that has been bound to a reference. Example:

int n = 0;
struct C {
  C(int) {}
  C(const C&) { ++n; } // 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 = 42; // copy-initialization, calls C::C(42) _or_ C::C( C(42) )
 
  return n; // returns either 0 or 1, depending on whether the copy was elided
}

The standard also mentions that a similar optimization may be applied to objects being thrown and caught,[3][4] but it is unclear whether the optimization applies to both the copy from the thrown object to the exception object, and the copy from the exception object to the object declared in the exception-declaration of the catch clause. It is also unclear whether this optimization only applies to temporary objects, or named objects as well.[5] Given the following source code:

#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.
}          // It is unclear whether this copy may be elided.
 
int main() {
  try {
    f();
  }
  catch(C c) {  // copying the exception object into the temporary in the exception declaration.
  }             // It is also unclear whether this copy may be elided.
}

A conforming compiler should therefore produce a program that prints "Hello World!" twice. In the upcoming C++ standard (C++0x), the issues have been addressed, essentially allowing both the copy from the named object to the exception object, and the copy into the object declared in the exception handler to be elided.[5]

References

  1. ^ ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §12.8 Copying class objects [class.copy] para. 15
  2. ^ Sutter, Herb (2001). More Exceptional C++. Addison-Wesley. 
  3. ^ ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §15.1 Throwing an exception [except.throw] para. 5
  4. ^ ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §15.3 Handling an exception [except.handle] para. 17
  5. ^ a b "C++ Standard Core Language Defect Reports". WG21. http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#479. Retrieved 2009-03-27. 

Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Return value optimization — Return value optimization, or simply RVO, is a compiler optimization technique that involves eliminating the temporary object created to hold a function s return value.[1] In C++, it is particularly notable for being allowed to change the… …   Wikipedia

  • Apostrophe — redirects here. For other uses, see (disambiguation). Apostrophes redirects here. For the music book, see Apostrophes: A Book of Tributes to Masters of Music. For other uses, see Apostrophe (disambiguation). ’ Apostrophe …   Wikipedia

  • Old Norse — dǫnsk tunga, dansk tunga ( Danish tongue ), norrœnt mál ( Norse language ) Spoken in Nordic countries, Scotland, Ireland, England and Wales, Isle of Man, Normandy, Vinland, the Volga and places in between …   Wikipedia

  • Music of Australia — Arts in Australia Culture of Australia Architecture Art Cinema Comic books Cuisine Dance …   Wikipedia

  • Concepts (C++) — Concepts and the related notion of axioms were an extension to C++ s template system proposed for C++0x. They were designed to improve compiler diagnostics and to allow programmers to codify in the program some formal properties of templates that …   Wikipedia

  • Old Japanese — 上古日本語, 上代日本語 Spoken in Japan Era Evolved into Early Middle Japanese during the Heian period Language family …   Wikipedia

  • Taos phonology — The main description of Taos phonology was contributed by George L. Trager in a (pre generative) structuralist framework. Earlier considerations of the phonetics phonology were by John P. Harrington and Jaime de Angulo. [de Angulo s work includes …   Wikipedia

  • IBN JANĀḤ, JONAH — (Abu al Walid Marwan; first half of 11thcentury), Spanish Hebrew grammarian and Hebrew lexicographer. In his writings Ibn Janāḥ refers to himself in various ways: by his full name (Lumaʿ, 19), by Abu al Walid (ibid., 169, 284), by Marwan… …   Encyclopedia of Judaism

  • Biblical Hebrew — Biblical Hebrew, Classical Hebrew שְֹפַת כְּנַעַן, יְהוּדִית, (לְשוֹן) עִבְרִית …   Wikipedia

  • John Barbour (poet) — John Barbour (?1320 ndash; March 13, 1395), was a Scottish poet and the first major literary voice to write in Scots, the vernacular language of Lowland Scotland, similar to the position that Chaucer, his slightly later contemporary,… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”