Move assignment operator
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. |
Ein Umzug Zuweisungsoperator der Klasse
T
ist eine Non-Vorlage nicht-statische Member-Funktion mit dem Namen operator=, die genau einen Parameter vom Typ T&&, const T&&, volatile T&& oder constvolatile T&& dauert. Ein Typ mit einem öffentlichen Zug Zuweisungsoperator ist MoveAssignable
.Original:
A move assignment operator of class
T
is a non-template non-static member function with the name operator= that takes exactly one parameter of type T&&, const T&&, volatile T&&, or constvolatile T&&. A type with a public move assignment operator is MoveAssignable
.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]Syntax
class_name& class_name:: operator= ( class_name&& ) | (1) | (seit C++11) | |||||||
class_name& class_name:: operator= ( class_name&& ) = default; | (2) | (seit C++11) | |||||||
class_name& class_name:: operator= ( class_name&& ) = delete; | (3) | (seit C++11) | |||||||
[Bearbeiten]Erklärung
# Typische Deklaration einer Bewegung Zuweisungsoperator
Original:
# Typical declaration of a move assignment operator
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.
# Erzwingen ein Umzug Zuweisungsoperator vom Compiler erzeugt werden
Original:
# Forcing a move assignment operator to be generated by the compiler
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.
# Vermeiden impliziten move Zuordnung
Original:
# Avoiding implicit move assignment
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.
Der Umzug Zuweisungsoperator wird aufgerufen, wenn es durch Überlast Auflösung, zB ausgewählt wenn ein Objekt auf der linken Seite einer Zuweisung Ausdruck, wo die rechte Seite ist ein rvalue der gleichen oder implizit konvertierbar Typ erscheint .
Original:
The move assignment operator is called whenever it is selected by Überlast Auflösung, e.g. when an object appears on the left side of an assignment expression, where the right-hand side is an rvalue of the same or implicitly convertible type.
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.
Bewegen Zuweisungsoperatoren typischerweise "stehlen" die Ressourcen mit dem Argument (zB Zeiger auf dynamisch zugewiesenen Objekte, Dateideskriptoren TCP-Sockets, I / O-Streams, laufenden Threads, etc) gehalten, anstatt Kopien von ihnen, und lassen Sie das Argument in einigen gültig, aber ansonsten unbestimmten Zustand. Zum Beispiel lässt move-Zuweisung von einem std::string oder von einem std::vector der rechten Seite Argument leer .
Original:
Move assignment operators typically "steal" the resources held by the argument (e.g. pointers to dynamically-allocated objects, file descriptors, TCP sockets, I/O streams, running threads, etc), rather than make copies of them, and leave the argument in some valid but otherwise indeterminate state. For example, move-assigning from a std::string or from a std::vector leaves the right-hand side argument empty.
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]Implizit deklarierte Bewegung Zuweisungsoperator
Wenn kein Benutzer-definierte Bewegung Zuweisungsoperatoren sind für eine Klasse-Typ (struct, class oder union) vorgesehen ist und alle folgenden Bedingungen erfüllt ist:
Original:
If no user-defined move assignment operators are provided for a class type (struct, class, or union), and all of the following is true:
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.
- Es gibt noch keine deklarierte Kopie KonstruktorenOriginal:there are no user-declared copy constructorsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Es gibt noch keine deklarierte move-KonstruktorenOriginal:there are no user-declared move constructorsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Es gibt keine vom Anwender deklariert Kopie ZuweisungsoperatorenOriginal:there are no user-declared copy assignment operatorsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Es gibt noch keine deklarierte DestruktorenOriginal:there are no user-declared destructorsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - die implizit deklarierte Bewegung Zuweisungsoperator nicht definiert als gelöschtOriginal:the implicitly-declared move assignment operator would not be defined as deletedThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
dann wird der Compiler einen Umzug Zuweisungsoperator als
inline public
Mitglied seiner Klasse mit der Unterschrift
erklären Original:
then the compiler will declare a move assignment operator as an
inline public
member of its class with the signature
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.
Eine Klasse kann mehrere move Zuweisungsoperatoren, zB sowohl T& T::operator=(const T&&) und T& T::operator=(T&&). Wenn einige benutzerdefinierte move Zuweisungsoperatoren vorhanden sind, kann der Benutzer noch zwingen die Erzeugung des implizit deklarierten move Zuweisungsoperator mit dem Schlüsselwort
default
.Original:
A class can have multiple move assignment operators, e.g. both T& T::operator=(const T&&) and T& T::operator=(T&&). If some user-defined move assignment operators are present, the user may still force the generation of the implicitly declared move assignment operator with the keyword
default
.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.
Da einige Zuweisungsoperator (Verschieben oder Kopieren) wird immer für jede Klasse deklariert wird, wird die Basisklasse Zuweisungsoperator immer versteckt. Wenn eine using-Deklaration wird verwendet, um in der Zuweisungsoperator von der Basisklasse zu bringen, und das Argument type könnte das gleiche wie das Argument Typ des impliziten Zuweisungsoperator der abgeleiteten Klasse zu sein, wird die using-Deklaration auch von der impliziten versteckt Erklärung .
Original:
Because some assignment operator (move or copy) is always declared for any class, the base class assignment operator is always hidden. If a using-declaration is used to bring in the assignment operator from the base class, and its argument type could be the same as the argument type of the implicit assignment operator of the derived class, the using-declaration is also hidden by the implicit declaration.
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]Gelöschte implizit deklariert move Zuweisungsoperator
Die implizit deklariert oder ausgefallen move Zuweisungsoperator für die Klasse
T
ist definiert als gelöscht in einer der folgenden Punkte zutrifft:Original:
The implicitly-declared or defaulted move assignment operator for class
T
is defined as deleted in any of the following is true: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.
T
einen nicht-statisches Datenelement, das const istOriginal:T
has a non-static data member that is constThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
hat eine nicht-statische Daten Mitglied einer Referenz-Typ .Original:T
has a non-static data member of a reference type.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
hat eine nicht-statische Daten Mitglied, die sich nicht bewegen kann zugewiesen werden (gelöscht hat, unzugänglich oder mehrdeutig move Zuweisungsoperator)Original:T
has a non-static data member that cannot be move-assigned (has deleted, inaccessible, or ambiguous move assignment operator)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
hat direkte oder virtuelle Basisklasse, die sich nicht bewegen kann zugewiesen werden (gelöscht hat, unzugänglich oder mehrdeutig move Zuweisungsoperator)Original:T
has direct or virtual base class that cannot be move-assigned (has deleted, inaccessible, or ambiguous move assignment operator)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
hat eine nicht-statische Daten Mitglied oder eine direkte oder virtuelle Basis, ohne eine Bewegung Zuweisungsoperator, das nicht trivial kopierbar .Original:T
has a non-static data member or a direct or virtual base without a move assignment operator that is not trivially copyable.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
hat eine direkte oder indirekte virtuelle BasisklasseOriginal:T
has a direct or indirect virtual base classThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten]Trivial move Zuweisungsoperator
Die implizit deklariert move Zuweisungsoperator für die Klasse
T
ist trivial, wenn alle der folgenden Bedingungen erfüllt ist:Original:
The implicitly-declared move assignment operator for class
T
is trivial if all of the following is true: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.
T
hat keine virtuelle Member-FunktionenOriginal:T
has no virtual member functionsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.T
hat keine virtuellen BasisklassenOriginal:T
has no virtual base classesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.- Der Umzug Zuweisungsoperator für jede direkte Basis von
T
gewählt ist trivialOriginal:The move assignment operator selected for every direct base ofT
is trivialThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Der Umzug Zuweisungsoperator für jede nicht-statische Klasse-Typ (oder ein Array von Klasse-Typ) memeber der
T
gewählt ist trivialOriginal:The move assignment operator selected for every non-static class type (or array of class type) memeber ofT
is trivialThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Eine triviale move Zuweisungsoperator führt die gleiche Aktion wie die triviale Kopie assignmentoperator, also macht, ist eine Kopie der Objekt-Repräsentation wie von std::memmove. Alle Datentypen kompatibel mit der Programmiersprache C (POD-Typen) sind trivial zu bewegen zuweisbare .
Original:
A trivial move assignment operator performs the same action as the trivial copy assignmentoperator, that is, makes a copy of the object representation as if by std::memmove. All data types compatible with the C language (POD types) are trivially move-assignable.
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]Move Zuweisungsoperator implizit definiert
Wenn die implizit deklariert move Zuweisungsoperator nicht gelöscht oder trivial, ist es definiert (das heißt, eine Funktion Körper erzeugt und kompiliert) durch den Compiler. Für union Typen, kopiert die implizit definierte Bewegung Zuweisungsoperator die Objekt-Repräsentation (wie std::memmove). Für Nicht-union-Klasse-Typen (class und struct), führt der Umzug Zuweisungsoperator Vollmitglied-kluger Schachzug Zuordnung des Objekts Basen und nicht-statische Mitglieder, in deren Initialisierung Reihenfolge mit eingebauter Zuordnung für die Skalare und bewegen Zuweisungsoperator für Klasse Typen .
Original:
If the implicitly-declared move assignment operator is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler. For union types, the implicitly-defined move assignment operator copies the object representation (as by std::memmove). For non-union class types (class and struct), the move assignment operator performs full member-wise move assignment of the object's bases and non-static members, in their initialization order, using built-in assignment for the scalars and move assignment operator for class types.
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
Wenn beide kopieren und verschieben Zuweisungsoperatoren vorgesehen sind, wählt Überladungsauflösung den Umzug Zuordnung, wenn das Argument ein rvalue (entweder prvalue wie eine namenlose temporäre oder xWert, wie das Ergebnis der std::move ), und wählt den Zuweisungsoperator, wenn das Argument lvalue (benannte Objekt oder eine Funktion / Betreiber wieder lvalue Referenz). Wenn nur die Kopie Zuordnung haben, wählen Sie alle Argumente Kategorien es (wie lange es dauert ihr Argument als Wert oder als Verweis auf const, da rvalues const Referenzen binden können), was Zuweisungsoperator das Fallback für unterwegs Zuordnung beim Bewegen nicht verfügbar ist .
Original:
If both copy and move assignment operators are provided, overload resolution selects the move assignment if the argument is an rvalue (either prvalue such as a nameless temporary or xvalue such as the result of std::move), and selects the copy assignment if the argument is lvalue (named object or a function/operator returning lvalue reference). If only the copy assignment is provided, all argument categories select it (as long as it takes its argument by value or as reference to const, since rvalues can bind to const references), which makes copy assignment the fallback for move assignment, when move is unavailable.
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.
Der Copy-and-Swap Zuweisungsoperator
Original:
The copy-and-swap assignment operator
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.
T& T::operator=(T arg){
swap(arg);
return*this;
}
führt ein Äquivalent bewegen Zuordnung für rvalue Argumente auf Kosten einer zusätzlichen Aufruf der Bewegung der T Konstruktor, der oft annehmbaren .
Original:
performs an equivalent of move assignment for rvalue arguments at the cost of one additional call to the move constructor of T, which is often acceptable.
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 <string>#include <iostream>#include <utility> struct A {std::string s; A(): s("test"){} A(const A& o): s(o.s){std::cout<<"move failed!\n";} A(A&& o): s(std::move(o.s)){} A& operator=(const A&){std::cout<<"copy assigned\n";return*this;} A& operator=(A&& other){ s = std::move(other.s);std::cout<<"move assigned\n";return*this;}}; A f(A a){return a;} struct B : A {std::string s2;int n;// implicit move assignment operator B& B::operator=(B&&)// calls A's move assignment operator// calls s2's move assignment operator// and makes a bitwise copy of n}; struct C : B { ~C(){};// destructor prevents implicit move assignment}; struct D : B { D(){} ~D(){};// destructor would prevent implicit move assignment D& operator=(D&&)=default;// force a move assignment anyway }; int main(){ A a1, a2;std::cout<<"Trying to move-assign A from rvalue temporary\n"; a1 = f(A());// move-assignment from rvalue temporarystd::cout<<"Trying to move-assign A from xvalue\n"; a2 = std::move(a1);// move-assignment from xvalue std::cout<<"Trying to move-assign B\n"; B b1, b2;std::cout<<"Before move, b1.s = \""<< b1.s<<"\"\n"; b2 = std::move(b1);// calls implicit move assignmentstd::cout<<"After move, b1.s = \""<< b1.s<<"\"\n"; std::cout<<"Trying to move-assign C\n"; C c1, c2; c2 = std::move(c1);// calls the copy assignment operator std::cout<<"Trying to move-assign E\n"; D d1, d2; d2 = std::move(d1);}
Output:
Trying to move-assign A from rvalue temporary move assigned Trying to move-assign A from xvalue move assigned Trying to move-assign B Before move, b1.s = "test" move assigned After move, b1.s = "" Trying to move-assign C copy assigned Trying to move-assign E move assigned