copy elision
Da cppreference.com.
![]() |
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.
La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
Ottimizza le copia e spostare-costruttori, con conseguente zero-copy pass-by-value semantica.
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.
Indice |
[modifica] Spiegazione
Nei seguenti casi, i compilatori sono autorizzati a omettere il copia-e-si muovono costruttori di oggetti di classe, anche se copiare / spostare costruttore e il distruttore sono osservabili effetti collaterali.
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.
- Se una funzione restituisce un tipo di classe per valore, e l'espressione della dichiarazione return è il nome di un non-volatile oggetto con durata di memorizzazione automatica, che non è il parametro di funzione o un parametro clausola catch, e che ha lo stesso cv- tipo non qualificato come tipo di ritorno della funzione, quindi copiare / spostare viene omesso. Quando la variabile locale è costruito, è costruito direttamente nella memoria dove il valore restituito dalla funzione altrimenti essere spostati o copiati. Questa variante di elisione copia viene definita NRVO ", dal nome valore di ottimizzazione di ritorno".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.
- Quando un temporaneo senza nome, non legato ad alcun riferimento, sarebbe stato spostato o copiato in un oggetto dello stesso cv-tipo non qualificato, la copia / spostamento viene omesso. Quando temporanea che è costruito, è costruito direttamente nella memoria in cui sarebbe altrimenti essere spostato o copiato. Quando la temporanea senza nome è l'argomento di un'istruzione return, questa variante di elisione copia viene definita RVO, "tornare ottimizzazione valore".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 laterale espressione, se l'operando è il nome di una non-volatile oggetto con durata di memorizzazione automatica, che non è il parametro funzione o un parametro clausola catch, e il cui campo di applicazione non si estende oltre il più interno tenta-blocco ( se vi è una prova-block), poi copia / spostamento viene omesso. Quando la variabile locale è costruito, è costruito direttamente nella memoria in cui l'oggetto eccezione altrimenti essere spostati o copiati. (dal 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. (dal C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Quando si maneggiano un'eccezione, se l'argomento della clausola catch è dello stesso tipo (ad eccezione di cv-qualifica) come oggetto eccezione generata, la copia / spostamento viene omesso e il corpo della clausola catch accede all'oggetto di eccezione, come se è stato passato per riferimento. (dal 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. (dal C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Elisioni copia multipla possono essere concatenati per eliminare le copie.
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.
[modifica] Note
Copia elisione è l'unica forma consentita di ottimizzazione che è possibile modificare le osservabili effetti collaterali. Poiché alcuni compilatori non si esegue elisione copia in tutte le situazioni dove è permesso, i programmi che si basano sugli effetti collaterali di copiare / spostare i costruttori e distruttori non sono portatili.
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.
Anche quando elisione copia avviene e la copy-/move-constructor non viene chiamato, deve essere presente e accessibile, altrimenti il programma è mal formato.
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.
[modifica] Esempio
#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