std::move
Da 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. |
Definido no cabeçalho <utility>
|
||
template< class T > typename std::remove_reference<T>::type&& move( T&& t ); |
(desde C++11) | |
std::move
obtém uma referência rvalue a sua tese. Rvalue referências são de outra produzido apenas por objetos temporários, assim o código da biblioteca que passou uma referência rvalue para um objeto de recurso de proprietários tem a opção (mas não é obrigatório) para mover' o recurso fora do argumento, a fim de executar mais rapidamente, deixando o argumento com um valor vazio. O código da biblioteca é necessário para deixar um valor válido no argumento, mas a menos que os documentos do tipo ou função de outra forma, não há outras restrições sobre o valor do argumento resultante. Isto significa que é mais prudente, para evitar geralmente usando um argumento movidos de novo. Se você tem que usá-lo novamente, certifique-se de re-inicializar com um valor conhecido antes de fazer isso.Original:
std::move
obtains an rvalue reference to its argument. Rvalue references are otherwise only produced by temporary objects, so library code that's passed an rvalue reference to a resource-owning object has the option (but isn't required) to move the resource out of the argument in order to run more quickly, leaving the argument with an empty value. The library code is required to leave a valid value in the argument, but unless the type or function documents otherwise, there are no other constraints on the resulting argument value. This means that it's generally wisest to avoid using a moved from argument again. If you have to use it again, be sure to re-initialize it with a known value before doing so.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.
Índice |
[editar] Parâmetros
t | - | o objeto a ser movido
Original: the object to be moved The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[editar] Valor de retorno
static_cast<typename std::remove_reference<T>::type&&>(t)
[editar] Exceções
[editar] Exemplo
#include <iostream> #include <utility> #include <vector> #include <string> int main() { std::string str = "Hello"; std::vector<std::string> v; // uses the push_back(const T&) overload, which means // we'll incur the cost of copying str v.push_back(str); std::cout << "After copy, str is \"" << str << "\"\n"; // uses the rvalue reference push_back(T&&) overload, // which means no strings will copied; instead, the contents // of str will be moved into the vector. This is less // expensive, but also means str might now be empty. v.push_back(std::move(str)); std::cout << "After move, str is \"" << str << "\"\n"; std::cout << "The contents of the vector are \"" << v[0] << "\", \"" << v[1] << "\"\n"; }
Saída:
After copy, str is "Hello" After move, str is "" The contents of the vector are "Hello", "Hello"
[editar] Complexidade
Constante
Original:
Constant
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.
[editar] Veja também
(C++11) |
envio de uma função Original: forwards a function argument The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) |
(C++11) |
obtém uma referência rvalue se o construtor movimento não joga Original: obtains an rvalue reference if the move constructor does not throw The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de função) |