Move constructors
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 costruttore mossa di
T
classe non è un modello di costruzione il cui primo parametro è T&&, const T&&, volatile T&& o const volatile T&&, e sia non ci sono altri parametri, o il resto dei parametri di tutti hanno valori di default. Un tipo con un costruttore mossa pubblico MoveConstructible
.Original:
A move constructor of class
T
is a non-template constructor whose first parameter is T&&, const T&&, volatile T&&, or const volatile T&&, and either there are no other parameters, or the rest of the parameters all have default values. A type with a public move constructor is MoveConstructible
.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 && )
|
(1) | (dal C++11) | |||||||
class_name ( class_name && ) = default;
|
(2) | (dal C++11) | |||||||
class_name ( class_name && ) = delete;
|
(3) | (dal C++11) | |||||||
[modifica] Spiegazione
# Dichiarazione tipica di un costruttore mossa
Original:
# Typical declaration of a move constructor
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 costruttore mossa per essere generato dal compilatore
Original:
# Forcing a move constructor 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 costruttore implicito mossa
Original:
# Avoiding implicit move constructor
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.
Il costruttore mossa è chiamata quando un oggetto è inizializzato da xValue dello stesso tipo, che comprende
Original:
The move constructor is called whenever an object is initialized from xvalue of the same type, which includes
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.
- inizializzazione, T a = std::move(b); o T a(std::move(b));, dove b è di tipo
T
Original:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Passaggio di argomenti funzione: f(std::move(a));, dove
a
è di tipoT
ef
è void f(T t)Original:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - di ritorno della funzione: return a; all'interno di una funzione, ad esempio T f(), dove
a
è diT
tipo che ha un costruttore mossa.Original:function return: return a; inside a function such as T f(), wherea
is of typeT
which has a move constructor.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Spostare costruttori tipicamente "rubare" le risorse contenute l'argomento (ad esempio i puntatori a oggetti allocati dinamicamente, descrittori di file, socket TCP, I / O ruscelli, thread in esecuzione, ecc), piuttosto che farne delle copie, e lasciare l'argomento in uno stato valido ma per il resto indeterminato. Ad esempio, passando da un std::string o da un std::vector trasforma l'argomento vuoto.
Original:
Move constructors 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, moving from a std::string or from a std::vector turns the 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 costruttore mossa
Se non definite dall'utente costruttori di movimento vengono forniti per un tipo di classe (struct, class o union), e tutte le seguenti condizioni:
Original:
If no user-defined move constructors 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 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 dall'utente dichiarato operatori di assegnazione di spostamentoOriginal:there are no user-declared move 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 destructursOriginal:there are no user-declared destructursThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - il costruttore mossa implicitamente dichiarato-non potrebbe essere definita come cancellatiOriginal:the implicitly-declared move constructor 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 dichiarare un costruttore mossa come membro
inline public
della sua classe con la T::T(T&&)
firma Original:
then the compiler will declare a move constructor as an
inline public
member of its class with the signature T::T(T&&)
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 costruttori si muovono di più, ad es sia T::T(const T&&) e T::T(T&&). Se alcuni definiti dall'utente costruttori movimento sono presenti, l'utente può comunque forzare la generazione del costruttore mossa implicitamente dichiarata con la parola chiave
default
.Original:
A class can have multiple move constructors, e.g. both T::T(const T&&) and T::T(T&&). If some user-defined move constructors are present, the user may still force the generation of the implicitly declared move constructor 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.
[modifica] Soppresso implicitamente dichiarato costruttore mossa
Il costruttore mossa implicitamente dichiarate o inadempiente per
T
classe è definita come cancellati in una qualsiasi delle seguenti condizioni:Original:
The implicitly-declared or defaulted move constructor 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 membri non statici di dati che non possono essere spostati (sono state eliminate, inaccessibili, o costruttori spostare ambigue)Original:T
has non-static data members that cannot be moved (have deleted, inaccessible, or ambiguous move constructors)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 possono essere spostati (ha eliminato, inaccessibile, o costruttori spostare ambigue)Original:T
has direct or virtual base class that cannot be moved (has deleted, inaccessible, or ambiguous move constructors)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 con un distruttore cancellati o inaccessibiliOriginal:T
has direct or virtual base class with a deleted or inaccessible destructorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. -
T
ha definito dall'utente costruttore movimento o operatore di assegnazione di spostamentoOriginal:T
has a user-defined move constructor or move assignment operatorThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. -
T
è un sindacato e ha un membro variante con non banale costruttore di copiaOriginal:T
is a union and has a variant member with non-trivial copy constructorThe 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 o di una base diretta o virtuale senza un costruttore mossa che non è banalmente copiabili.Original:T
has a non-static data member or a direct or virtual base without a move constructor 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.
[modifica] Mossa costruttore Trivial
Il costruttore mossa implicitamente dichiarato per-
T
classe è banale se tutte le seguenti condizioni:Original:
The implicitly-declared move constructor 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. - Il costruttore mossa selezionata per ogni base diretta di
T
è banaleOriginal:The move constructor 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. - Il costruttore mossa selezionati per ogni non-static tipo di classe (o una matrice di tipo di classe) memeber di
T
è banaleOriginal:The move constructor 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 costruttore mossa banale è un costruttore che esegue la stessa azione, come il costruttore di 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 mobili.
Original:
A trivial move constructor is a constructor that performs the same action as the trivial copy constructor, 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 movable.
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 costruttore mossa
Se il costruttore mossa implicitamente dichiarato-non viene eliminato o banale, è definito (cioè corpo di una funzione viene generato e compilato) da parte del compilatore. Per i tipi union, il costruttore mossa implicitamente definito copia la rappresentazione dell'oggetto (come da std::memmove). Per i non-sindacali tipi di classe (class e struct), il costruttore mossa esegue membro a pieno titolo-mossa saggia di basi dell'oggetto e membri non statici, nel loro ordine di inizializzazione, utilizzando l'inizializzazione diretta con un argomento xValue.
Original:
If the implicitly-declared move constructor 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 constructor copies the object representation (as by std::memmove). For non-union class types (class and struct), the move constructor performs full member-wise move of the object's bases and non-static members, in their initialization order, using direct initialization with an xvalue argument.
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
Per rendere garanzia eccezione forte possibile, definite dall'utente costruttori movimento non deve generare eccezioni. In realtà, contenitori standard tipicamente si basano su std::move_if_noexcept di scegliere tra movimento e copia quando elementi contenitori devono essere trasferiti.
Original:
To make strong exception guarantee possible, user-defined move constructors should not throw exceptions. In fact, standard containers typically rely on std::move_if_noexcept to choose between move and copy when container elements need to be relocated.
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 entrambi i costruttori di copia e spostamento sono previsti, risoluzione dell'overload seleziona il costruttore mossa se l'argomento è un rvalue (o prvalue come una temporanea senza nome o xValue, come il risultato di std::move) , e seleziona il costruttore di copia se l'argomento è lvalue' (oggetto con nome o di una funzione / operatore di ritorno valore assegnabile di riferimento). Se solo il costruttore di copia è previsto, tutte le categorie di argomenti selezionarlo (come tempo necessario riferimento a const, in quanto rvalues può legarsi a riferimenti const), il che rende la copia del fallback per lo spostamento, quando si muove non è più disponibile.
Original:
If both copy and move constructors are provided, overload resolution selects the move constructor 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 constructor if the argument is lvalue (named object or a function/operator returning lvalue reference). If only the copy constructor is provided, all argument categories select it (as long as it takes reference to const, since rvalues can bind to const references), which makes copying the fallback for moving, when moving 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.
In molte situazioni, i costruttori movimento sono ottimizzati fuori anche se produrrebbero osservabili effetti collaterali, copia elisione vedere
Original:
In many situations, move constructors are optimized out even if they would produce observable side-effects, see copia elisione
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> 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 f(A a) { return a; } struct B : A { std::string s2; int n; // implicit move-contructor B::(B&&) // calls A's move constructor // calls s2's move constructor // and makes a bitwise copy of n }; struct C : B { ~C() {}; // destructor prevents implicit move }; struct D : B { D() {} ~D() {}; // destructor would prevent implicit move D(D&&) = default; // force a move ctor anyway }; int main() { std::cout << "Trying to move A\n"; A a1 = f(A()); // move-construct from rvalue temporary A a2 = std::move(a1); // move-construct from xvalue std::cout << "Trying to move B\n"; B b1; std::cout << "Before move, b1.s = \"" << b1.s << "\"\n"; B b2 = std::move(b1); // calls implicit move ctor std::cout << "After move, b1.s = \"" << b1.s << "\"\n"; std::cout << "Trying to move C\n"; C c1; C c2 = std::move(c1); // calls the copy constructor std::cout << "Trying to move D\n"; D d1; D d2 = std::move(d1); }
Output:
Trying to move A Trying to move B Before move, b1.s = "test" After move, b1.s = "" Trying to move C move failed! Trying to move D