Move assignment operator
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
一招赋值操作符类
T
operator=,T&&,const T&&,或volatile T&&类型的一个参数的名称const volatile T&&是一个非模板的非静态成员函数。与公众移动赋值运算符是A型MoveAssignable
.原文:
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
.目录 |
[编辑] 语法
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 起) | |||||||
[编辑] 解释
#典型的一招赋值运算符的声明
原文:
# Typical declaration of a move assignment operator
#强制,此举由编译器生成的赋值运算符
原文:
# Forcing a move assignment operator to be generated by the compiler
#避免隐式移动分配
此举被称为赋值运算符时,它选择重载决议,例如:一个对象时,会出现在一个赋值表达式的左侧,其中的右手侧的相同的或隐式转换的类型是一个rvalue.
原文:
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.
移动赋值操作符典型的“窃取”的参数(例如动态分配的对象的指针,文件描述符,TCP套接字I / O流,正在运行的线程等)的资源,而不是让它们的副本,以及离开的说法在一些有效的,但其他不确定的状态。例如,将分配从std::string或从std::vector叶的右手边参数为空.
原文:
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.
[编辑] 隐式声明的移动赋值运算符
如果没有提供用户定义的移动赋值运算符的类类型(struct,class,或union),以下是正确的:
原文:
If no user-defined move assignment operators are provided for a class type (struct, class, or union), and all of the following is true:
- 有没有用户声明的拷贝构造函数原文:there are no user-declared copy constructors
- 有没有用户声明的move构造函数原文:there are no user-declared move constructors
- 有没有用户声明的拷贝赋值运算符原文:there are no user-declared copy assignment operators
- 有没有用户声明析构函数原文:there are no user-declared destructors
- 隐式声明的移动赋值操作符就不会被定义为删除原文:the implicitly-declared move assignment operator would not be defined as deleted
那么编译器会声明的举动分配,运营商
inline public
类成员的签名
原文:
then the compiler will declare a move assignment operator as an
inline public
member of its class with the signature
一个类可以有多个移动赋值运算符,如: T& T::operator=(const T&&)和T& T::operator=(T&&)。如果一些用户自定义的移动赋值操作符都存在,用户仍然可以强制生成的隐式声明的移动赋值操作符与关键字
default
.原文:
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
.由于一些赋值运算符(移动或复制)的任何类,基类的赋值运算符总是宣称永远是隐藏的。如果一个声明是用来把从基类中的赋值运算符,它的参数类型可以是相同的参数类型的隐式派生类的赋值运算符,声明还隐藏着由隐声明.
原文:
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.
[编辑] 删除隐式声明的移动赋值运算符
隐式声明的或默认的移动赋值操作符类
T
被定义为“删除”以下是正确的:原文:
The implicitly-declared or defaulted move assignment operator for class
T
is defined as deleted in any of the following is true:-
T
有一个非静态数据成员是const原文:T
has a non-static data member that is const -
T
有一个非静态数据成员的引用类型.原文:T
has a non-static data member of a reference type. -
T
有一个非静态数据成员不能移动分配(已删除,无法访问,或暧昧的举动赋值运算符)原文:T
has a non-static data member that cannot be move-assigned (has deleted, inaccessible, or ambiguous move assignment operator) -
T
有直接或虚基类,不能移动分配(已删除,无法访问,或暧昧的举动赋值运算符)原文:T
has direct or virtual base class that cannot be move-assigned (has deleted, inaccessible, or ambiguous move assignment operator) -
T
有一个非静态数据成员或直接或虚基类,没有一招赋值运算符,是不平凡复制的.原文:T
has a non-static data member or a direct or virtual base without a move assignment operator that is not trivially copyable. -
T
有直接或间接的虚基类原文:T
has a direct or indirect virtual base class
[编辑] 琐碎的举动赋值运算符
隐式声明的移动赋值操作符类
T
以下是微不足道的,如果是真实的原文:
The implicitly-declared move assignment operator for class
T
is trivial if all of the following is true:-
T
没有虚成员函数 -
T
没有虚基类 - 选择每一个直接基础
T
的移动赋值运算符是微不足道的原文:The move assignment operator selected for every direct base ofT
is trivial - 选择每一个非静态类类型(或类类型的数组)星期四会员
T
的移动赋值运算符是微不足道的原文:The move assignment operator selected for every non-static class type (or array of class type) memeber ofT
is trivial
一个平凡的举动赋值运算符执行相同的简单复制assignmentoperator的行动,就是使对象表示,如果由std::memmove的副本。所有兼容的数据类型与C语言(POD类型)是平凡的移动可分配的
原文:
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.
[编辑] 隐式定义的移动赋值运算符
如果隐式声明的移动赋值运算符是不会被删除或微不足道的,它的定义,函数体生成和编译的编译器。 union类型,隐式定义的移动赋值操作符复制对象表示(std::memmove)的。对于非工会类类型(class和struct),在移动分配运营商进行完整的成员,明智的举动分配的对象的基础和非静态成员,在他们的初始化顺序,使用内置的在分配的标量和移动赋值操作符类的类型.
原文:
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.
[编辑] 注释
如果复制和移动赋值运算符重载解析选择移动分配的说法是“右值”(或者“prvalue”如一个无名的临时或“xvalue”的结果std::move ),并选择拷贝赋值的说法是“左值”(命名对象或函数/操作符返回左值参考)。如果仅仅是拷贝赋值,所有的参数类别选择(只要它采取它的参数的值或为const,因为右值绑定到const引用),这使得拷贝赋值移动分配的回退,当移动是不可用
原文:
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 copy-and-swap assignment operator
T& T::operator=(T arg) {
swap(arg);
return *this;
}
执行分配一个额外通话费用的右值参数相当于移动的移动构造函数T,这往往是可以接受的.
原文:
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.
[编辑] 示例
#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); }
输出:
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