copy elision
De 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. |
<metanoindex/>
Otimiza a copiar e mover-construtores, resultando em zero cópia de passagem por valor semântica.
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.
Explicação
Sob as seguintes circunstâncias, os compiladores são permitidas para omitir a cópia e movimento construtores dos objetos de classe, mesmo que copiar / mover construtor e destrutor ter observáveis efeitos colaterais.
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 uma função retorna um tipo de classe por valor, e de expressão da declaração
returné o nome de um objecto não-volátil, com duração de armazenamento automático, que não é um parâmetro da função, ou um parâmetro cláusula de captura, e que tem a mesma cv- Tipo incondicional como o tipo de retorno da função, em seguida, copiar / mover é omitido. Quando essa variável local é construído, ele é construído diretamente no armazenamento onde o valor de retorno da função, caso contrário podem ser movidos ou copiados. Esta variante do elisão cópia é conhecida como NRVO ", nome otimização valor de retorno".Original:If a function returns a class type by value, and thereturnstatement'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 um temporário sem nome, não vinculado a quaisquer referências, seria movido ou copiado para um objeto do tipo CV-incondicional mesmo, o copiar / mover é omitido. Quando isso temporária é construída, ela é construída diretamente no armazenamento onde seriam movidos ou copiados. Quando o temporário sem nome é o argumento de uma instrução de retorno, esta variante de elisão cópia é conhecida como RVO, "voltar a otimização de valor".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.
- Em um lance-expressão, se o operador é o nome de um objeto não-volátil, com duração de armazenamento automático, o que não é um parâmetro da função, ou um parâmetro de cláusula de captura, e cujo alcance não se estende além do mais íntimo bloco try-( se houver uma tentativa de bloco), então a cópia / movimento é omitido. Quando essa variável local é construído, ele é construído diretamente no armazenamento onde o objeto de exceção seriam movidos ou copiados. (desde 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. (desde C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Ao manusear uma exceção, se o argumento da cláusula de captura é do mesmo tipo (exceto para cv-qualificação) como o objeto de exceção lançada, o copiar / mover é omitida e o corpo da cláusula catch acessa o objeto de excepção, como se foi passado por referência. (desde 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. (desde C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Elisões de cópias múltiplas podem ser encadeados para eliminar várias cópias.
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.
Notas
Cópia elisão é a única forma permitida de otimização que pode alterar as observáveis efeitos colaterais. Porque alguns compiladores não realizam elisão cópia em qualquer situação onde é permitido, programas que dependem os efeitos colaterais de copiar / mover construtores e destruidores não são portáteis.
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.
Mesmo quando elisão cópia tem lugar e o copy-/move-constructor não é chamado, ele deve estar presente e acessível, caso contrário, o programa está mal formada.
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.
Exemplo
#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()
}
Potencial saída:
constructed
constructed
constructed
constructed
constructed
constructed
arg.size() = 3
destructed
destructed
destructed
destructed
destructed
destructed