copy elision
Aus cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Optimiert von copy-and move-Konstruktoren, was zero-copy pass-by-value-Semantik .
Original:
Optimizes out copy- and move-constructors, resulting in zero-copy pass-by-value semantics.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Inhaltsverzeichnis |
[Bearbeiten] Erklärung
Unter den folgenden Umständen werden die Compiler gestattet, die copy-and move-Konstruktoren der Klasse Objekte auch wenn copy / move Konstruktor und der Destruktor haben Nebenwirkungen beobachtet weglassen .
Original:
Under the following circumstances, the compilers are permitted to omit the copy- and move-constructors of class objects even if copy/move constructor and the destructor have observable side-effects.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- Wenn eine Funktion eine Klasse Art von Wert, und die return Anweisung Ausdruck ist der Name eines nicht-flüchtigen Objekt mit automatischer Lagerdauer, die nicht die Funktion Parameter oder eine catch-Klausel Parameter und die die gleiche ist cv- unqualifizierte Typ wie der Rückgabetyp der Funktion, dann Kopieren / Verschieben wird ausgelassen. Wenn das lokale Variable konstruiert wird, wird es direkt in dem Speicher, wo der Funktion Rückgabewert anderweitig bewegt würde oder kopiert werden gebaut. Diese Variante der Kopie elision als NRVO bekannt ist ", benannt Rückgabewert Optimierung" .Original:If a function returns a class type by value, and the return statement's expression is the name of a non-volatile object with automatic storage duration, which isn't the function parameter, or a catch clause parameter, and which has the same cv-unqualified type as the return type of the function, then copy/move is omitted. When that local variable is constructed, it is constructed directly in the storage where the function's return value would otherwise be moved or copied to. This variant of copy elision is known as NRVO, "named return value optimization".The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Wenn eine namenlose temporäre, nicht für alle Verweise gebunden, würde verschoben oder in ein Objekt des gleichen cv-unqualifizierten Typs kopiert werden, wird das Kopieren / Verschieben weggelassen. Wenn dieser temporären aufgebaut ist, wird es direkt in der Speichereinheit aufgebaut, wo es anderweitig bewegt würde oder kopiert werden. Wenn der namenlose temporäre das Argument einer return-Anweisung ist, diese Variante der Kopie elision als RVO bekannt ist, "Rückgabewert Optimierung" .Original:When a nameless temporary, not bound to any references, would be moved or copied into an object of the same cv-unqualified type, the copy/move is omitted. When that temporary is constructed, it is constructed directly in the storage where it would otherwise be moved or copied to. When the nameless temporary is the argument of a return statement, this variant of copy elision is known as RVO, "return value optimization".The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- In einer Einwurf-Expression, wenn der Operand der Name eines nichtflüchtigen Objekt mit dynamischem Speicher Dauer, die nicht der Funktionsparameter oder ein Fang-Klausel Parameter und deren Umfang nicht über die innerste Try-Block erstrecken beträgt ( wenn es einen try-Block), dann copy / move wird ausgelassen. Wenn das lokale Variable konstruiert wird, wird es direkt in dem Speicher, wo das Exception-Objekt anderweitig bewegt würde oder kopiert werden gebaut. (seit C++11)Original:In a throw-expression, if the operand is the name of a non-volatile object with automatic storage duration, which isn't the function parameter, or a catch clause parameter, and whose scope does not extend past the innermost try-block (if there is a try-block), then copy/move is omitted. When that local variable is constructed, it is constructed directly in the storage where the exception object would otherwise be moved or copied to. (seit C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Beim Umgang mit eine Ausnahme, wenn das Argument der catch-Klausel ist von der gleichen Art (mit Ausnahme von cv-Qualifikation) als die Ausnahme-Objekt ausgelöst, wird das Kopieren / Verschieben weggelassen und der Körper der catch-Klausel greift die Ausnahme Objekt direkt, wie wenn es per Referenz übergeben. (seit C++11)Original:When handling an exception, if the argument of the catch clause is of the same type (except for cv-qualification) as the exception object thrown, the copy/move is omitted and the body of the catch clause accesses the exception object directly, as if it was passed by reference. (seit C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Mehrfachkopien Auslassungen können verkettet werden, um mehrere Kopien zu beseitigen .
Original:
Multiple copy elisions may be chained to eliminate multiple copies.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten] Notes
Kopieren Auslassung ist die einzige erlaubte Form der Optimierung, die die beobachtbaren Nebenwirkungen ändern können. Da einige Compiler nicht durchführen copy elision in jeder Situation, wo es erlaubt ist, sind Programme, die auf die Nebenwirkungen von copy / move Konstruktoren und Destruktoren verlassen nicht tragbar .
Original:
Copy elision is the only allowed form of optimization that can change the observable side-effects. Because some compilers do not perform copy elision in every situation where it is allowed, programs that rely on the side-effects of copy/move constructors and destructors are not portable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Auch beim Kopieren elision stattfindet und die copy-/move-constructor wird nicht aufgerufen, es muss vorhanden und zugänglich, ansonsten ist das Programm schlecht gebildet .
Original:
Even when copy elision takes place and the copy-/move-constructor is not called, it must be present and accessible, otherwise the program is ill-formed.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten] Beispiel
#include <vector> #include <iostream> struct Noisy { Noisy() {std::cout << "constructed\n"; } Noisy(const Noisy&) { std::cout << "copied\n"; } Noisy(Noisy&&) { std::cout << "moved\n"; } ~Noisy() {std::cout << "destructed\n"; } }; std::vector<Noisy> f() { std::vector<Noisy> v = std::vector<Noisy>(3); // copy elision from temporary to v return v; // NRVO from v to the nameless temporary that is returned } void fn_by_val(std::vector<Noisy> arg) { std::cout << "arg.size() = " << arg.size() << '\n'; } int main() { std::vector<Noisy> v = f(); // copy elision from returned temporary to v fn_by_val(f()); // and from temporary to the argument of fn_by_val() }
Possible output:
constructed constructed constructed constructed constructed constructed arg.size() = 3 destructed destructed destructed destructed destructed destructed