[ Web Proxy ]
URL:
Viewing: https://fr.cppreference.com/cpp/language/move_constructor [Back]  [Original]

Move constructors cppreference.com
cppreference.com
Espaces de noms
Variantes

Move constructors

De cppreference.com

<metanoindex/>

 
 
Langage C++
Sujets gnraux
Contrle de flux
Instructions conditionnelles
Instructions d'itration
Instructions de saut
Fonctions
dclaration de fonction
expression lambda
fonction gnrique
spcificateur inline
spcification d'exception (obsolte)
spcificateur noexcept (C++11)
Exceptions
Espaces de noms
Types
spcificateur decltype (C++11)
Qualificatifs
qualificatifs const et volatile
qualificatifs de stockage
qualificatif constexpr (C++11)
qualificatif auto (C++11)
qualificatif alignas (C++11)
Initialisation
Littraux
Expressions
oprateurs alternatifs
Utilitaires
Types
dclaration typedef
dclaration d'alias de type (C++11)
attributs (C++11)
Jette
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversions implicites
conversion const_cast
conversion static_cast
conversion dynamic_cast
conversion reinterpret_cast
conversions style C et style fonction
Allocation de mmoire
Classes
Qualificatifs spcifiques aux membres de classe
Fonctions membres spciales
Modles
classes gnriques
fonctions gnriques
spcialisation de modles
paquets de paramtres (C++11)
Divers
Assembleur
 
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.

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.
# 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.
# 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.
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.
  • l'initialisation, ou T a = std::move(b); T a(std::move(b));, o b est de T type
    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.
  • passage argument de fonction: f(std::move(a));, o a est de type T et f est 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.
  • retour de la fonction: return a; l'intrieur d'une fonction telle que T f(), o a est T type qui a un constructeur mouvement .
    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.
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.

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.
  • il n'ya pas de constructeur de copie utilisateur-dclares
    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.
  • n'ya pas d'oprateurs dclars par l'utilisateur, affectation par copie
    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.
  • n'ya pas d'oprateurs dclars par l'utilisateur affectation dplacer
    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.
  • il n'y a pas destructurs utilisateur-dclares
    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.
  • le constructeur mouvement implicitement dclar ne serait pas dfini comme tant supprim
    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.
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.
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.

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.
  • T a des membres de donnes non statiques qui ne peuvent pas tre dplacs (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 dplac (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 inaccessible
    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 a un constructeur dfini par l'utilisateur ou l'oprateur d'affectation dmnagement dmnagement
    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 est une union et comporte un lment variante avec le constructeur de copie non trivial
    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 a un membre non statique de donnes 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.

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.
  • T n'a pas de fonctions membres virtuelles
    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 a pas de classes de base virtuelles
    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.
  • Le constructeur mouvement slectionn pour chaque base directe de T est trivial
    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.
  • Le constructeur mouvement slectionn pour chaque type de classe non statique (ou un tableau de type de classe) memeber de T est trivial
    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 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.

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.

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

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

Web Proxy Viewer  |  New URL  |  Original Page