Move constructors
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
一招构造函数的类
T
是一个非模板构造函数的第一个参数是T&&,const T&&,volatile T&&,或const volatile T&&,以及有没有其他的参数,其余的参数都具有默认值。与公众移动的构造函数是A型MoveConstructible
.原文:
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
.目录 |
[编辑] 语法
class_name ( class_name && )
|
(1) | (C++11 起) | |||||||
class_name ( class_name && ) = default;
|
(2) | (C++11 起) | |||||||
class_name ( class_name && ) = delete;
|
(3) | (C++11 起) | |||||||
[编辑] 解释
#典型的一招构造函数的声明
原文:
# Typical declaration of a move constructor
#强制构造函数被编译器生成的一招
原文:
# Forcing a move constructor to be generated by the compiler
#避免隐含的转移构造函数
从xvalue同一类型的,其中包括初始化的移动对象时,构造函数被调用
原文:
The move constructor is called whenever an object is initialized from xvalue of the same type, which includes
- 函数的返回:return a;内的功能,如T f(),
a
是其中有一招构造函数的类型T
.原文:function return: return a; inside a function such as T f(), wherea
is of typeT
which has a move constructor.
将构造函数通常是“偷”的资源举行的参数(例如动态分配的对象的指针,文件描述符,TCP套接字I / O流,正在运行的线程,等等),而不是让它们的副本,以及离开的说法一些有效的,但其他不确定的状态。例如,从std::string移动或std::vector从原来的参数为空.
原文:
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.
[编辑] 隐式声明的移动构造函数
如果没有用户定义的移动构造函数的类类型(struct,class,或union),以下是真实的
原文:
If no user-defined move constructors are provided for a class type (struct, class, or union), and all of the following is true:
- 有没有用户声明的拷贝构造函数原文:there are no user-declared copy constructors
- 有没有用户声明的拷贝赋值运算符原文:there are no user-declared copy assignment operators
- 有没有用户声明的移动赋值运算符原文:there are no user-declared move assignment operators
- 有没有用户声明destructurs原文:there are no user-declared destructurs
- 隐式声明的移动构造函数不能被定义为删除原文:the implicitly-declared move constructor would not be defined as deleted
那么编译器会声明的举动构造
inline public
类成员的签名T::T(T&&)
原文:
then the compiler will declare a move constructor as an
inline public
member of its class with the signature T::T(T&&)
一个类可以有多个移动的构造函数,例如T::T(const T&&)和T::T(T&&)。如果一些用户自定义的移动构造函数,用户仍然可以强制生成的隐式声明的移动构造函数的关键字
default
.原文:
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
.[编辑] 删除隐式声明的移动构造函数
类
T
隐式声明的或默认的移动构造函数被定义为“删除”以下是正确的:原文:
The implicitly-declared or defaulted move constructor for class
T
is defined as deleted in any of the following is true:-
T
非静态数据成员,不能移动(已删除,无法访问,或暧昧的举动构造函数)原文:T
has non-static data members that cannot be moved (have deleted, inaccessible, or ambiguous move constructors) -
T
有直接或虚基类,它不能移动(已删除,无法访问,或暧昧的举动构造函数)原文:T
has direct or virtual base class that cannot be moved (has deleted, inaccessible, or ambiguous move constructors) -
T
有直接或虚基类被删除或无法访问的析构函数原文:T
has direct or virtual base class with a deleted or inaccessible destructor -
T
有一个用户定义的移动构造函数或移动赋值运算符原文:T
has a user-defined move constructor or move assignment operator -
T
是工会的成员不平凡的拷贝构造函数有一个变体原文:T
is a union and has a variant member with non-trivial copy constructor -
T
有一个非静态数据成员或直接或虚基类没有移动的构造函数是不平凡复制的.原文:T
has a non-static data member or a direct or virtual base without a move constructor that is not trivially copyable.
[编辑] 琐碎的移动构造函数
移动构造函数隐式声明的类
T
以下是微不足道的,如果是真实的原文:
The implicitly-declared move constructor for class
T
is trivial if all of the following is true:-
T
没有虚成员函数 -
T
没有虚基类 - 选择每一个直接基础
T
的移动构造函数是微不足道的原文:The move constructor selected for every direct base ofT
is trivial - 每一个非静态类类型(或类类型的数组)星期四会员
T
选择此举构造,实在是微不足道原文:The move constructor selected for every non-static class type (or array of class type) memeber ofT
is trivial
一个平凡的移动构造函数是一个构造函数执行相同的操作琐碎的拷贝构造函数,也就是副本的对象表示,如果由std::memmove。所有兼容的数据类型与C语言(POD类型)是平凡的移动.
原文:
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.
[编辑] 隐式定义的移动构造函数
如果隐式声明的移动构造函数不会被删除或微不足道的,它的定义,函数体生成和编译的编译器。 union类型,隐式定义的移动构造函数复制对象表示(std::memmove)的。对于非工会的类类型(class和struct),此举构造函数执行对象的基地和非静态成员的正式成员明智的举动,在他们的初始化顺序,使用直接初始化与xvalue参数的.
原文:
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.
[编辑] 注释
为了使强异常保证,移动用户定义的构造函数不应该抛出异常。事实上,标准容器通常依赖于std::move_if_noexcept之间选择,移动和复制容器中的元素时,需要搬迁.
原文:
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.
如果复制和移动的构造,重载构造函数,如果参数是选择移动的“右值”(或者“prvalue”如一个无名的临时或“xvalue”,如std::move的结果) ,并选择拷贝构造函数的说法是“左值”(命名对象或函数/操作符返回左值参考)。如果仅仅是拷贝构造函数,所有的参数类别选择(只要它为const,因为右值绑定到const引用),这使得复制后备移动,移动时是不可用
原文:
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.
在许多情况下,移动构造函数进行了优化,即使他们能观察到的副作用,副本省略
原文:
In many situations, move constructors are optimized out even if they would produce observable side-effects, see 副本省略
[编辑] 示例
#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); }
输出:
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