Move assignment operator
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. |
Un operatore di assegnazione mossa di
T
classe è un non-modello non statico funzione membro con il nome che prende operator= esattamente un parametro di tipo T&&, const T&&, volatile T&& o const volatile T&&. Un tipo con un operatore pubblico di assegnazione mossa è 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 const volatile 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.
Indice |
[modifica] Sintassi
class_name & class_name :: operator= ( class_name && )
|
(1) | (dal C++11) | |||||||
class_name & class_name :: operator= ( class_name && ) = default;
|
(2) | (dal C++11) | |||||||
class_name & class_name :: operator= ( class_name && ) = delete;
|
(3) | (dal C++11) | |||||||
[modifica] Spiegazione
# Dichiarazione tipica di un operatore di assegnamento mossa
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.
# Forzare un operatore di assegnazione mossa per essere generato dal compilatore
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.
# Evitare l'assegnazione implicita mossa
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.
L'operatore di assegnazione mossa è chiamata ogni volta che viene selezionato dal sovraccarico risoluzione, ad esempio, quando un oggetto appare sul lato sinistro di un'espressione di assegnazione, dove il lato destro è una rvalue dello stesso tipo o implicitamente convertibile.
Original:
The move assignment operator is called whenever it is selected by sovraccarico risoluzione, 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.
Spostare operatori di assegnazione tipicamente "rubare" le risorse contenute l'argomento (ad esempio i puntatori a oggetti allocati dinamicamente, descrittori di file, socket TCP, I / O, i flussi thread in esecuzione, ecc), piuttosto che farne delle copie, e lasciare l'argomento in uno stato valido ma altrimenti indeterminata. Per esempio, spostare-assegnazione da un std::string o da un std::vector lascia la destra argomento lato vuoto.
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.
[modifica] Implicitamente-ha dichiarato un operatore di assegnazione mossa
Se nessun utente definite operatori di assegnazione spostamento sono previsti per un tipo di classe (struct, class o union), e tutte le seguenti condizioni:
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.
- non ci sono dichiarate dall'utente costruttori di copiaOriginal: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. - non ci sono dichiarate dall'utente costruttori movimentoOriginal: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. - non ci sono utenti-operatori di assegnamento copia dichiarataOriginal: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. - non ci sono dichiarate dall'utente distruttoriOriginal: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. - il implicitamente dichiarato-operatore di assegnazione mossa non potrebbe essere definita come cancellatiOriginal: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.
allora il compilatore dichiara un operatore di assegnamento mossa come un membro
inline public
della sua classe con la
firma 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.
Una classe può avere più operatori di assegnazione di movimento, come ad esempio sia T& T::operator=(const T&&) e T& T::operator=(T&&). Se alcuni definiti dall'utente operatori di assegnazione si muovono sono presenti, l'utente può comunque forzare la generazione dell'operatore mossa implicitamente dichiarato di assegnazione con la parola chiave
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.
Poiché alcuni operatore di assegnazione (spostamento o copia) è sempre dichiarato per ogni classe, la classe di operatore di base di assegnazione è sempre nascosto. Se una dichiarazione con-viene utilizzato per portare in l'operatore di assegnamento dalla classe base, e il suo tipo di argomento potrebbe essere lo stesso del tipo di argomento dell'operatore di assegnazione implicita della classe derivata, la dichiarazione con-viene anche nascosta dalla implicita dichiarazione.
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.
[modifica] Soppresso implicitamente dichiarato operatore di assegnazione di spostamento
Il implicitamente dichiarate o default operatore di assegnazione di movimento per
T
classe è definita come cancellati in una qualsiasi delle seguenti condizioni: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
ha un membro non statico di dati che è constOriginal: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
ha un membro non statico di dati di un tipo di riferimento.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
ha un membro non statico di dati che non possono essere spostare assegnato (ha eliminato, inaccessibile, o ambigua operatore di assegnazione mossa)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
ha classe base diretta o virtuale che non è possibile spostare assegnato (ha eliminato, inaccessibile, o ambigua operatore di assegnazione mossa)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
ha un non-membro static dati o da una base diretta o virtuale senza un operatore di assegnamento mossa che non è banalmente copiabili.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
ha una classe direttamente o indirettamente base virtualeOriginal: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.
[modifica] Trivial mossa operatore di assegnazione
Il implicitamente dichiarato-operatore di assegnazione di movimento per
T
classe è banale se tutte le seguenti condizioni: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
non ha funzioni membro virtualiOriginal: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
non ha classi base virtualiOriginal: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. - L'operatore di assegnazione mossa selezionati per ogni base diretta di
T
è banaleOriginal: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. - L'operatore di assegnazione mossa selezionati per ogni non-static tipo di classe (o una matrice di tipo di classe) memeber di
T
è banaleOriginal: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.
Un banale operatore di assegnazione movimento esegue la stessa azione del assignmentoperator copia banale, che è, fa una copia della rappresentazione dell'oggetto come per std::memmove. Tutti i tipi di dati compatibili con il linguaggio C (tipo POD) sono banalmente spostare assegnabili.
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.
[modifica] Implicitamente definito operatore di assegnazione di spostamento
Se il implicitamente dichiarato operatore di assegnazione mossa non viene eliminato o banale, è definito (cioè corpo di una funzione viene generato e compilato) da parte del compilatore. Per i tipi union, implicitamente definito l'operatore di assegnazione mossa copia la rappresentazione dell'oggetto (come da std::memmove). Per i non-sindacali tipi di classe (class e struct), l'operatore di assegnazione move esegue membro a pieno titolo-saggio assegnazione mossa basi dell'oggetto e membri non statici, nel loro ordine di inizializzazione, con built-in assegnazione per gli scalari e assegnazione move per i tipi di classe.
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.
[modifica] Note
Se sia copiare e spostare gli operatori di assegnazione vengono forniti, la risoluzione dell'overload seleziona l'assegnazione mossa se l'argomento è un rvalue (o prvalue come una temporanea senza nome o xValue, come il risultato di std::move ), e seleziona l'assegnazione copia se l'argomento è lvalue' (dal nome di oggetto o di una funzione / operatore di ritorno valore assegnabile di riferimento). Se solo l'assegnazione copia sia presente, tutte le categorie di argomenti selezionarlo (il tempo che prende il suo argomento per valore o per riferimento a const, in quanto rvalues può legarsi a riferimenti const), che fa assegnamento per copia di riserva per l'assegnazione del movimento, quando si sposta non è più disponibile.
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.
La copia-e-swap operatore di assegnazione
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;
}
esegue un equivalente di assegnazione mossa per gli argomenti rvalue al costo di una chiamata supplementare al costruttore mossa di T, che spesso è accettabile.
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.
[modifica] Esempio
#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 temporary std::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 assignment std::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