Move assignment operator
来自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. |
一招赋值操作符类
Toperator=,T&&,const T&&,或volatile T&&类型的一个参数的名称const volatile T&&是一个非模板的非静态成员函数。与公众移动赋值运算符是A型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.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 语法
class_name & class_name :: operator= ( class_name && )
|
(1) | (C++11 起) | |||||||
class_name & class_name :: operator= ( class_name && ) = default;
|
(2) | (C++11 起) | |||||||
class_name & class_name :: operator= ( class_name && ) = delete;
|
(3) | (C++11 起) | |||||||
[编辑] 解释
#典型的一招赋值运算符的声明
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.
You can help to correct and verify the translation. Click here for instructions.
#强制,此举由编译器生成的赋值运算符
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.
You can help to correct and verify the translation. Click here for instructions.
#避免隐式移动分配
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.
You can help to correct and verify the translation. Click here for instructions.
此举被称为赋值运算符时,它选择重载决议,例如:一个对象时,会出现在一个赋值表达式的左侧,其中的右手侧的相同的或隐式转换的类型是一个rvalue.
Original:
The move assignment operator is called whenever it is selected by 重载决议, 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.
You can help to correct and verify the translation. Click here for instructions.
移动赋值操作符典型的“窃取”的参数(例如动态分配的对象的指针,文件描述符,TCP套接字I / O流,正在运行的线程等)的资源,而不是让它们的副本,以及离开的说法在一些有效的,但其他不确定的状态。例如,将分配从std::string或从std::vector叶的右手边参数为空.
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.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 隐式声明的移动赋值运算符
如果没有提供用户定义的移动赋值运算符的类类型(struct,class,或union),以下是正确的:
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.
You can help to correct and verify the translation. Click here for instructions.
- 有没有用户声明的拷贝构造函数Original: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. - 有没有用户声明的move构造函数Original:there are no user-declared move constructorsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 有没有用户声明的拷贝赋值运算符Original: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. - 有没有用户声明析构函数Original:there are no user-declared destructorsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 隐式声明的移动赋值操作符就不会被定义为删除Original:the implicitly-declared move assignment operator 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.
那么编译器会声明的举动分配,运营商
inline public类成员的签名 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.
You can help to correct and verify the translation. Click here for instructions.
一个类可以有多个移动赋值运算符,如: T& T::operator=(const T&&)和T& T::operator=(T&&)。如果一些用户自定义的移动赋值操作符都存在,用户仍然可以强制生成的隐式声明的移动赋值操作符与关键字
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.
You can help to correct and verify the translation. Click here for instructions.
由于一些赋值运算符(移动或复制)的任何类,基类的赋值运算符总是宣称永远是隐藏的。如果一个声明是用来把从基类中的赋值运算符,它的参数类型可以是相同的参数类型的隐式派生类的赋值运算符,声明还隐藏着由隐声明.
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.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 删除隐式声明的移动赋值运算符
隐式声明的或默认的移动赋值操作符类
T被定义为“删除”以下是正确的: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.
You can help to correct and verify the translation. Click here for instructions.
-
T有一个非静态数据成员是constOriginal:Thas a non-static data member that is constThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. -
T有一个非静态数据成员的引用类型.Original:Thas 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有一个非静态数据成员不能移动分配(已删除,无法访问,或暧昧的举动赋值运算符)Original:Thas 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有直接或虚基类,不能移动分配(已删除,无法访问,或暧昧的举动赋值运算符)Original:Thas 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有一个非静态数据成员或直接或虚基类,没有一招赋值运算符,是不平凡复制的.Original:Thas 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有直接或间接的虚基类Original:Thas a direct or indirect virtual base classThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 琐碎的举动赋值运算符
隐式声明的移动赋值操作符类
T以下是微不足道的,如果是真实的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.
You can help to correct and verify the translation. Click here for instructions.
-
T没有虚成员函数Original: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. -
T没有虚基类Original: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. - 选择每一个直接基础
T的移动赋值运算符是微不足道的Original:The move assignment operator 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. - 选择每一个非静态类类型(或类类型的数组)星期四会员
T的移动赋值运算符是微不足道的Original:The move assignment operator 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.
一个平凡的举动赋值运算符执行相同的简单复制assignmentoperator的行动,就是使对象表示,如果由std::memmove的副本。所有兼容的数据类型与C语言(POD类型)是平凡的移动可分配的
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.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 隐式定义的移动赋值运算符
如果隐式声明的移动赋值运算符是不会被删除或微不足道的,它的定义,函数体生成和编译的编译器。 union类型,隐式定义的移动赋值操作符复制对象表示(std::memmove)的。对于非工会类类型(class和struct),在移动分配运营商进行完整的成员,明智的举动分配的对象的基础和非静态成员,在他们的初始化顺序,使用内置的在分配的标量和移动赋值操作符类的类型.
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.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 注释
如果复制和移动赋值运算符重载解析选择移动分配的说法是“右值”(或者“prvalue”如一个无名的临时或“xvalue”的结果std::move ),并选择拷贝赋值的说法是“左值”(命名对象或函数/操作符返回左值参考)。如果仅仅是拷贝赋值,所有的参数类别选择(只要它采取它的参数的值或为const,因为右值绑定到const引用),这使得拷贝赋值移动分配的回退,当移动是不可用
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.
You can help to correct and verify the translation. Click here for instructions.
复制和交换赋值运算符
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.
You can help to correct and verify the translation. Click here for instructions.
T& T::operator=(T arg) {
swap(arg);
return *this;
}
执行分配一个额外通话费用的右值参数相当于移动的移动构造函数T,这往往是可以接受的.
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.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 为例
#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); }
Output:
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