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

Move assignment operator cppreference.com
cppreference.com
Espaces de noms
Variantes

Move assignment operator

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 oprateur d'affectation de dplacement T classe est une fonction non-template lment non-statique avec le nom operator= qui prend exactement un paramtre de type de T&&, const T&&, volatile T&&, ou const volatile T&&. Un type avec un oprateur d'affectation mouvement public est MoveAssignable .
Original:
A move assignment operator of class T is a non-template non-static member function with the name operator= that takes exactly one parameter of type T&&, const T&&, volatile T&&, or const volatile T&&. A type with a public move assignment operator is MoveAssignable.
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 :: operator= ( class_name && ) (1) (depuis C++11)
class_name & class_name :: operator= ( class_name && ) = default; (2) (depuis C++11)
class_name & class_name :: operator= ( class_name && ) = delete; (3) (depuis C++11)

Explication

# Dclaration typique d'un oprateur d'affectation mouvement
Original:
# Typical declaration of a 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.
# Forcer un oprateur d'affectation mouvement doit tre gnr par le compilateur
Original:
# Forcing a move assignment operator 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 d'affectation mouvement implicite
Original:
# Avoiding implicit move assignment
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
L'oprateur d'affectation mouvement est appel chaque fois qu'il est slectionn par rsolution de surcharge, par exemple, quand un objet apparat sur le ct gauche d'une expression d'affectation, o la droite est une rvalue du mme type ou implicitement convertible .
Original:
The move assignment operator is called whenever it is selected by rsolution de surcharge, e.g. when an object appears on the left side of an assignment expression, where the right-hand side is an rvalue of the same or implicitly convertible type.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Dplacez oprateurs d'affectation gnralement voler les ressources dtenues par l'argument (par exemple des pointeurs vers des objets dynamiquement alloues, les descripteurs de fichiers, sockets TCP, I / O flux, fils de course, etc), plutt que de faire des copies, et de laisser l'argument dans un tat valide, mais autrement indtermine. Par exemple, dplacez-attribution d'un std::string ou d'un std::vector reste l'argument de droite vide .
Original:
Move assignment operators 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, move-assigning from a std::string or from a std::vector leaves the right-hand side 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 oprateur d'affectation mouvement

Si aucun dfinis par l'utilisateur des oprateurs d'affectation mouvements sont prvus pour un type de classe (struct, class ou union), et tout ce qui suit est vrai:
Original:
If no user-defined move assignment operators 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.
  • il n'existe pas de constructeurs se dplacent utilisateur-dclares
    Original:
    there are no user-declared move 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.
  • il n'y a pas destructeurs utilisateur-dclares
    Original:
    there are no user-declared destructors
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • l'oprateur dmnagement implicitement dclar cession ne serait pas dfini comme tant supprim
    Original:
    the implicitly-declared move assignment operator 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 oprateur d'affectation mouvement en tant que membre inline public de sa classe avec le signature
Original:
then the compiler will declare a move assignment operator as an inline public member of its class with the signature
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 oprateurs d'affectation se dplacer, par exemple, la fois T& T::operator=(const T&&) et T& T::operator=(T&&). Si certains dfinies par l'utilisateur des oprateurs d'affectation mouvements sont prsents, l'utilisateur peut toujours forcer la gnration de l'oprateur d'affectation mouvement implicitement dclare avec le mot-cl default .
Original:
A class can have multiple move assignment operators, e.g. both T& T::operator=(const T&&) and T& T::operator=(T&&). If some user-defined move assignment operators are present, the user may still force the generation of the implicitly declared move assignment operator 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.
Parce que certains oprateur d'affectation (dplacement ou copie) est toujours fixe pour toute la classe, l'oprateur d'affectation de classe de base est toujours cach. Si une aide-dclaration est utilise pour amener l'oprateur d'affectation dans la classe de base, et de son type d'argument pourrait tre le mme que le type d'argument de l'oprateur d'affectation implicite de la classe drive, l'aide-dclaration est galement masque par l'implicite dclaration .
Original:
Because some assignment operator (move or copy) is always declared for any class, the base class assignment operator is always hidden. If a using-declaration is used to bring in the assignment operator from the base class, and its argument type could be the same as the argument type of the implicit assignment operator of the derived class, the using-declaration is also hidden by the implicit declaration.
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 oprateur d'affectation mouvement

L'oprateur dmnagement implicitement dclare ou dfaut d'affectation pour T classe est dfinie comme' supprims dans l'une des conditions suivantes est remplie:
Original:
The implicitly-declared or defaulted move assignment operator 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 un membre non statique de donnes qui est const
    Original:
    T has a non-static data member that is const
    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 d'un type de rfrence .
    Original:
    T has a non-static data member of a reference type.
    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 qui ne peut pas tre assign dplacer (a supprim, inaccessible, ou ambigus oprateur d'affectation mouvement)
    Original:
    T has a non-static data member that cannot be move-assigned (has deleted, inaccessible, or ambiguous 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 a de la classe de base directe ou virtuelle qui ne peut pas tre assign dplacer (a supprim, inaccessible, ou ambigus oprateur d'affectation mouvement)
    Original:
    T has direct or virtual base class that cannot be move-assigned (has deleted, inaccessible, or ambiguous 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 a un membre non statique de donnes ou une base directe ou virtuelle sans un oprateur d'affectation mouvement qui n'est pas trivialement copiable .
    Original:
    T has a non-static data member or a direct or virtual base without a move assignment operator 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.
  • T a une classe de base directe ou indirecte virtuelle
    Original:
    T has a direct or indirect virtual base class
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

Trivial oprateur d'affectation mouvement

L'oprateur dmnagement implicitement dclar cession T classe est triviale si l'ensemble des conditions suivantes est remplie:
Original:
The implicitly-declared move assignment operator 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.
  • L'oprateur d'affectation mouvement slectionn pour chaque base directe de T est trivial
    Original:
    The move assignment operator 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.
  • L'oprateur d'affectation mouvement slectionn pour chaque type de classe non statique (ou un tableau de type de classe) memeber de T est trivial
    Original:
    The move assignment operator 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 oprateur d'affectation mouvement trivial effectue la mme action que le assignmentoperator copie trivial, c'est--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 dplacer cessible .
Original:
A trivial move assignment operator performs the same action as the trivial copy assignmentoperator, 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 move-assignable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Implicitement dfini par l'oprateur d'affectation mouvement

Si l'oprateur dmnagement implicitement dclar affectation 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, l'oprateur dmnagement implicitement dfini par l'assignation copie reprsentation de l'objet (comme par std::memmove). Pour les types de classes non syndiqus (class et struct), l'oprateur d'affectation mouvement effectue plein d'affectation dmnagement membre-sage des bases de l'objet et non-membres statiques, dans leur ordre d'initialisation, l'aide intgre dans l'attribution des scalaires et oprateurs d'affectation mouvement pour les types de classe .
Original:
If the implicitly-declared move assignment operator 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 assignment operator copies the object representation (as by std::memmove). For non-union class types (class and struct), the move assignment operator performs full member-wise move assignment of the object's bases and non-static members, in their initialization order, using built-in assignment for the scalars and move assignment operator for class types.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Notes

Si les deux copies et les oprateurs d'affectation mouvements sont prvus, la rsolution de surcharge slectionne l'affectation dmnagement, si l'argument est un' rvalue (soit' prvalue comme un temporaire sans nom ou xValue comme le rsultat d'std::move ), et slectionne l'assignation copie si l'argument est lvalue' (du nom de l'objet ou une fonction / retour lvalue oprateur de rfrence). Si seulement l'affectation de copie est fournie, toutes les catgories d'arguments slectionnez-le (dans la mesure o il tire son argument par valeur ou rfrence const, depuis rvalues peut se lier des rfrences const), ce qui rend l'affectation de copie de secours pour l'affectation mouvement, quand se dplacer n'est pas disponible .
Original:
If both copy and move assignment operators are provided, overload resolution selects the move assignment 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 assignment if the argument is lvalue (named object or a function/operator returning lvalue reference). If only the copy assignment is provided, all argument categories select it (as long as it takes its argument by value or as reference to const, since rvalues can bind to const references), which makes copy assignment the fallback for move assignment, when move is unavailable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
L'oprateur d'affectation de copie-and-swap
Original:
The copy-and-swap 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& T::operator=(T arg) { swap(arg); return *this; }

effectue l'quivalent d'affectation mouvement pour les arguments rvalue au cot d'un appel supplmentaire au constructeur mouvement de T, ce qui est souvent acceptable .
Original:
performs an equivalent of move assignment for rvalue arguments at the cost of one additional call to the move constructor of T, which is often acceptable.
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>
#include <utility>

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& operator=(const A&) { std::cout << "copy assigned\n"; return *this; }
    A& operator=(A&& other) {
         s = std::move(other.s);
         std::cout << "move assigned\n";
         return *this;
    }
};

A f(A a) { return a; }

struct B : A {
     std::string s2; 
     int n;
     // implicit move assignment operator B& B::operator=(B&&)
     // calls A's move assignment operator
     // calls s2's move assignment operator
     // and makes a bitwise copy of n
};

struct C : B {
    ~C() {}; // destructor prevents implicit move assignment
};

struct D : B {
    D() {}
    ~D() {}; // destructor would prevent implicit move assignment
    D& operator=(D&&) = default; // force a move assignment anyway 
};

int main()
{
    A a1, a2;
    std::cout << "Trying to move-assign A from rvalue temporary\n";
    a1 = f(A()); // move-assignment from rvalue temporary
    std::cout << "Trying to move-assign A from xvalue\n";
    a2 = std::move(a1); // move-assignment from xvalue

    std::cout << "Trying to move-assign B\n";
    B b1, b2;
    std::cout << "Before move, b1.s = \"" << b1.s << "\"\n";
    b2 = std::move(b1); // calls implicit move assignment
    std::cout << "After move, b1.s = \"" << b1.s << "\"\n";

    std::cout << "Trying to move-assign C\n";
    C c1, c2;
    c2 = std::move(c1); // calls the copy assignment operator

    std::cout << "Trying to move-assign E\n";
    D d1, d2;
    d2 = std::move(d1);
}

Rsultat :

Trying to move-assign A from rvalue temporary
move assigned
Trying to move-assign A from xvalue
move assigned
Trying to move-assign B
Before move, b1.s = "test"
move assigned
After move, b1.s = "" 
Trying to move-assign C
copy assigned
Trying to move-assign E
move assigned

Web Proxy Viewer  |  New URL  |  Original Page