Namensräume
Varianten
Aktionen

std::move

Aus cppreference.com
< cpp‎ | utility


definiert in Header <utility>
template< class T >
typename std::remove_reference<T>::type&& move( T&& t );
(seit C++11)
std::move erhält eine rvalue Bezug auf ihr Argument. Rvalue Referenzen sind sonst nur durch temporäre Objekte erzeugt, so Bibliotheks-Code, der eine rvalue Verweis auf eine Ressource-besitzende Objekt übergeben wird hat die Möglichkeit (aber nicht verpflichtet), unterwegs die Ressource aus dem Argument, um zu laufen schneller, so dass das Argument mit einem leeren Wert. Die Bibliothek wird benötigt, um einen gültigen Wert im Argument zu verlassen, aber es sei denn, die Art oder Funktion Dokumente sonst gibt es keine weiteren Einschränkungen der resultierende Wert für das Argument. Dies bedeutet, dass es in der Regel am klügsten zu vermeiden, mit einem vom Argument bewegte sich wieder. Wenn Sie es wieder verwenden, achten Sie darauf neu zu initialisieren es mit einem bekannten Wert, bevor Sie so .
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.

Inhaltsverzeichnis

[Bearbeiten] Parameter

t -
das Objekt verschoben werden soll
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.

[Bearbeiten] Rückgabewert

static_cast<typename std::remove_reference<T>::type&&>(t)

[Bearbeiten] Ausnahmen

noexcept specification:  
noexcept
  (seit C++11)

[Bearbeiten] Beispiel

#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";
}

Output:

After copy, str is "Hello"
After move, str is ""
The contents of the vector are "Hello", "Hello"

[Bearbeiten] Komplexität

Constant
Original:
Constant
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Siehe auch

(C++11)
leitet eine Funktion Argument
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.

(Funktions-Template) [edit]
erhält eine rvalue Referenz, wenn der Umzug Konstruktor nicht werfen wird
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.

(Funktions-Template) [edit]