Namespace
Varianti

Move constructors

Da cppreference.com.
< cpp‎ | language

 
 
Linguaggio C + +
Temi generali
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Controllo del flusso
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Dichiarazioni esecuzione condizionale
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Iterazione dichiarazioni
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Vai dichiarazioni
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funzioni
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
dichiarazione di funzione
lambda funzione dichiarazione
funzione di modello
specificatore inline
eccezioni specifiche (deprecato)
noexcept specificatore (C++11)
Eccezioni
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Spazi dei nomi
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipi
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
Specifiers
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cv specificatori
Durata di stoccaggio specificatori
constexpr specificatore (C++11)
specificatore auto (C++11)
alignas specificatore (C++11)
Inizializzazione
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Letterali
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Espressioni
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rappresentazioni alternative
Utilities
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipi
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
Tipo alias dichiarazione (C++11)
attributi (C++11)
Lancia
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversioni implicite
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Fusione C-stile e funzionale
Occupazione della memoria
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classi
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Specifiche per una classe di funzioni proprietà
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
esplicito (C++11)
statico
Funzioni membro speciali
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
costruttore di default
costruttore di copia
spostare costruttore (C++11)
Modelli
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
classe template
funzione di modello
modello di specializzazione
parametri confezioni (C++11)
Varie
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Montaggio in linea
 
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.

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.
# 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.
# 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.
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.
  • inizializzazione, T a = std::move(b); o T a(std::move(b));, dove b è di tipo T
    Original:
    initialization, T a = std::move(b); or T a(std::move(b));, where b is of type T
    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 tipo T e f è void f(T t)
    Original:
    function argument passing: f(std::move(a));, where a is of type T and f is void f(T t)
    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 è di T tipo che ha un costruttore mossa.
    Original:
    function return: return a; inside a function such as T f(), where a is of type T 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.

[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.
  • non ci sono dichiarate dall'utente costruttori di copia
    Original:
    there are no user-declared copy constructors
    The 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 dichiarata
    Original:
    there are no user-declared copy assignment operators
    The 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 spostamento
    Original:
    there are no user-declared move assignment operators
    The 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 destructurs
    Original:
    there are no user-declared destructurs
    The 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 cancellati
    Original:
    the implicitly-declared move constructor would not be defined as deleted
    The 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.
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.

[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.
  • 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 inaccessibili
    Original:
    T has direct or virtual base class with a deleted or inaccessible destructor
    The 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 spostamento
    Original:
    T has a user-defined move constructor or 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 è un sindacato e ha un membro variante con non banale costruttore di copia
    Original:
    T is a union and has a variant member with non-trivial copy constructor
    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 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.
  • T non ha funzioni membro virtuali
    Original:
    T has no virtual member functions
    The 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 virtuali
    Original:
    T has no virtual base classes
    The 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 è banale
    Original:
    The move constructor selected for every direct base of T is trivial
    The 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 è banale
    Original:
    The move constructor selected for every non-static class type (or array of class type) memeber of T is trivial
    The 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.

[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.

[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.
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.
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.

[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