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. |
<metanoindex/>
Un constructeur mouvement de
T classe est un constructeur non-template dont le premier paramtre est T&&, const T&&, volatile T&& ou const volatile T&&, et soit il n'y a pas d'autres paramtres, ou dans le reste des paramtres ont tous des valeurs par dfaut. Un type avec un constructeur dmnagement 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.
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) | |||||||
Explication
# Dclaration 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 gnr 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 dplacement est appel chaque fois qu'un objet est initialis partir xValue du mme 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 deTtypeOriginal:initialization,T a = std::move(b);orT a(std::move(b));, where b is of typeTThe 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));, oaest de typeTetfestvoid f(T t)Original:function argument passing:f(std::move(a));, whereais of typeTandfisvoid 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'intrieur d'une fonction telle queT f(), oaestTtype qui a un constructeur mouvement .Original:function return:return a;inside a function such asT f(), whereais of typeTwhich 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.
Dplacez constructeurs gnralement voler les ressources dtenues par l'argument (par exemple des pointeurs vers des objets dynamiquement alloues, les descripteurs de fichiers, sockets TCP, E / S des ruisseaux, des fils en cours d'excution, etc), plutt que de faire des copies, et de laisser l'argument en un tat valide, mais autrement indtermine. 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.
Implicitement dclare constructeur dmnagement
Si aucun constructeur mouvements dfinis 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-dclaresOriginal: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'oprateurs dclars 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'oprateurs dclars par l'utilisateur affectation dplacerOriginal: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-dclaresOriginal: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 dclar ne serait pas dfini comme tant supprimOriginal: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 dclarer 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 dplacer, par exemple la fois
T::T(const T&&) et T::T(T&&). Si certains constructeurs se dplacent dfinis par l'utilisateur sont prsents, l'utilisateur peut toujours forcer la gnration du constructeur mouvement implicitement dclare 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.
Supprim implicitement dclare constructeur dmnagement
Le constructeur mouvement implicitement dclare ou dfaut de
T classe est dfinie comme' supprims 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.
Ta des membres de donnes non statiques qui ne peuvent pas tre dplacs (avez supprim, inaccessible, ou les constructeurs coup ambigu)Original:Thas 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.Ta de la classe de base directe ou virtuelle qui ne peut pas tre dplac (a supprim, inaccessible, ou les constructeurs coup ambigu)Original:Thas 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.Ta de la classe de base directe ou virtuelle avec un destructeur supprim ou inaccessibleOriginal:Thas 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.Ta un constructeur dfini par l'utilisateur ou l'oprateur d'affectation dmnagement dmnagementOriginal:Thas 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.Test une union et comporte un lment variante avec le constructeur de copie non trivialOriginal:Tis 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.Ta un membre non statique de donnes ou une base directe ou virtuelle sans un constructeur mouvement qui n'est pas trivialement copiable .Original:Thas 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.
Constructeur de dmnagement trivial
Le constructeur mouvement implicitement dclar 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.
Tn'a pas de fonctions membres virtuellesOriginal:Thas 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.Ta pas de classes de base virtuellesOriginal:Thas 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 slectionn pour chaque base directe de
Test trivialOriginal:The move constructor selected for every direct base ofTis 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 slectionn pour chaque type de classe non statique (ou un tableau de type de classe) memeber de
Test trivialOriginal:The move constructor selected for every non-static class type (or array of class type) memeber ofTis 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 mme action que le constructeur de copie trivial, qui est, en fait une copie de la reprsentation de l'objet comme par std::memmove. Tous les types de donnes 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.
Dfinition implicite constructeur dmnagement
Si le constructeur mouvement implicitement dclare n'est pas supprim ou trivial, il est dfini (c'est--corps d'une fonction est gnr et compil) par le compilateur. Pour les types de
union, le constructeur mouvement dfinition implicite copie la reprsentation de l'objet (comme par std::memmove). Pour les types de classes non syndiqus (class et struct), le constructeur effectue dmnagement membre part entire-sage dcision 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.
Notes
Pour rendre possible une forte garantie exception, les constructeurs se dplacent dfinies par l'utilisateur ne doit pas lever des exceptions. En fait, les conteneurs standards s'appuient gnralement sur std::move_if_noexcept de choisir entre dplacer et copier lorsque des lments conteneurs doivent tre rinstalles .
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 dplacement sont prvus, la rsolution de surcharge slectionne le constructeur dmnagement, si l'argument est un' rvalue (soit' prvalue comme un temporaire sans nom ou xValue comme le rsultat d'std::move) , et slectionne le constructeur de copie si l'argument est lvalue' (objet nomm ou une fonction / retour lvalue oprateur de rfrence). Si seulement le constructeur de copie est fournie, toutes les catgories d'arguments slectionnez (aussi longtemps qu'il le faudra rfrence const, depuis rvalues peut se lier des rfrences const), ce qui rend la copie du repli pour se dplacer, lors du dplacement 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 dplacent sont optimiss supprime, mme 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.
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);
}
Rsultat :
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