Move constructors
De 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. |
Un constructeur mouvement de
T
classe est un constructeur non-template dont le premier paramètre est T&&, const T&&, volatile T&& ou const volatile T&&, et soit il n'y a pas d'autres paramètres, ou dans le reste des paramètres ont tous des valeurs par défaut. Un type avec un constructeur déménagement public est 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.
Sommaire |
[modifier] Syntaxe
class_name ( class_name && )
|
(1) | (depuis C++11) | |||||||
class_name ( class_name && ) = default;
|
(2) | (depuis C++11) | |||||||
class_name ( class_name && ) = delete;
|
(3) | (depuis C++11) | |||||||
[modifier] Explication
# Déclaration typique d'un constructeur de mouvement
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.
# Forcer un constructeur mouvement doit être généré par le compilateur
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.
# Éviter constructeur mouvement implicite
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.
Le constructeur de déplacement est appelé chaque fois qu'un objet est initialisé à partir xValue du même type, qui comprend
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.
- l'initialisation, ou T a = std::move(b); T a(std::move(b));, où b est de
T
typeOriginal:initialization, T a = std::move(b); or T a(std::move(b));, where b is of typeT
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - passage argument de fonction: f(std::move(a));, où
a
est de typeT
etf
est void f(T t)Original:function argument passing: f(std::move(a));, wherea
is of typeT
andf
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. - retour de la fonction: return a; l'intérieur d'une fonction telle que T f(), où
a
estT
type qui a un constructeur mouvement .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.
Déplacez constructeurs généralement «voler» les ressources détenues par l'argument (par exemple des pointeurs vers des objets dynamiquement allouées, les descripteurs de fichiers, sockets TCP, E / S des ruisseaux, des fils en cours d'exécution, etc), plutôt que de faire des copies, et de laisser l'argument en un état valide, mais autrement indéterminée. Par exemple, le passage d'une std::string ou d'un std::vector transforme l'argument vide .
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.
[modifier] Implicitement déclarée constructeur déménagement
Si aucun constructeur mouvements définis par l'utilisateur sont fournis pour un type de classe (struct, class ou union), et tout ce qui suit est vrai:
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.
- il n'ya pas de constructeur de copie utilisateur-déclaréesOriginal: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. - n'ya pas d'opérateurs déclarés par l'utilisateur, affectation par copieOriginal: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. - n'ya pas d'opérateurs déclarés par l'utilisateur affectation déplacerOriginal: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. - il n'y a pas destructurs utilisateur-déclaréesOriginal: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. - le constructeur mouvement implicitement déclaré ne serait pas défini comme étant suppriméOriginal: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.
alors le compilateur déclarer un constructeur mouvement en tant que membre
inline public
de sa classe avec le T::T(T&&)
signature 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.
Une classe peut avoir plusieurs constructeurs se déplacer, par exemple à la fois T::T(const T&&) et T::T(T&&). Si certains constructeurs se déplacent définis par l'utilisateur sont présents, l'utilisateur peut toujours forcer la génération du constructeur mouvement implicitement déclarée avec le mot-clé
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.
[modifier] Supprimé implicitement déclarée constructeur déménagement
Le constructeur mouvement implicitement déclarée ou défaut de
T
classe est définie comme' supprimés dans l'une des conditions suivantes est remplie: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
a des membres de données non statiques qui ne peuvent pas être déplacés (avez supprimé, inaccessible, ou les constructeurs coup ambigu)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
a de la classe de base directe ou virtuelle qui ne peut pas être déplacé (a supprimé, inaccessible, ou les constructeurs coup ambigu)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
a de la classe de base directe ou virtuelle avec un destructeur supprimé ou inaccessibleOriginal: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
a un constructeur défini par l'utilisateur ou l'opérateur d'affectation déménagement déménagementOriginal: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
est une union et comporte un élément variante avec le constructeur de copie non trivialOriginal: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
a un membre non statique de données ou une base directe ou virtuelle sans un constructeur mouvement qui n'est pas trivialement copiable .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.
[modifier] Constructeur de déménagement trivial
Le constructeur mouvement implicitement déclaré pour
T
classe est triviale si l'ensemble des conditions suivantes est remplie: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
n'a pas de fonctions membres virtuellesOriginal: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
a pas de classes de base virtuellesOriginal: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. - Le constructeur mouvement sélectionné pour chaque base directe de
T
est trivialOriginal: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. - Le constructeur mouvement sélectionné pour chaque type de classe non statique (ou un tableau de type de classe) memeber de
T
est trivialOriginal: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 constructeur mouvement trivial est un constructeur qui effectue la même action que le constructeur de copie trivial, qui est, en fait une copie de la représentation de l'objet comme par std::memmove. Tous les types de données compatibles avec le langage C (types POD) sont trivialement mobile .
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.
[modifier] Définition implicite constructeur déménagement
Si le constructeur mouvement implicitement déclarée n'est pas supprimé ou trivial, il est défini (c'est-à-corps d'une fonction est généré et compilé) par le compilateur. Pour les types de union, le constructeur mouvement définition implicite copie la représentation de l'objet (comme par std::memmove). Pour les types de classes non syndiqués (class et struct), le constructeur effectue déménagement membre à part entière-sage décision de bases de l'objet et non-membres statiques, dans leur ordre d'initialisation, en utilisant l'initialisation directe avec un argument 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.
[modifier] Notes
Pour rendre possible une forte garantie exception, les constructeurs se déplacent définies par l'utilisateur ne doit pas lever des exceptions. En fait, les conteneurs standards s'appuient généralement sur std::move_if_noexcept de choisir entre déplacer et copier lorsque des éléments conteneurs doivent être réinstallées .
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.
Si les deux constructeurs de copie et de déplacement sont prévus, la résolution de surcharge sélectionne le constructeur déménagement, si l'argument est un' rvalue (soit' prvalue comme un temporaire sans nom ou xValue comme le résultat d'std::move) , et sélectionne le constructeur de copie si l'argument est lvalue' (objet nommé ou une fonction / retour lvalue opérateur de référence). Si seulement le constructeur de copie est fournie, toutes les catégories d'arguments sélectionnez (aussi longtemps qu'il le faudra référence à const, depuis rvalues peut se lier à des références const), ce qui rend la copie du repli pour se déplacer, lors du déplacement n'est pas disponible .
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.
Dans de nombreuses situations, les constructeurs se déplacent sont optimisés supprimée, même si elles produisent des effets secondaires observables, élision copie voir
Original:
In many situations, move constructors are optimized out even if they would produce observable side-effects, see élision copie
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.
[modifier] Exemple
#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); }
Résultat :
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