自动生成移动操作
在 C 11 中,移动语义的引入引发了关于移动构造函数和移动赋值的情况的问题运算符是自动生成的。
在 C 11 下,移动操作不会像复制构造函数和复制赋值那样自动生成运营商。编译器仅在满足某些条件时生成移动语义:
理解这些规则有助于指导利用移动语义的类的实现。 Howard Hinnant 演示文稿中的表格提供了自动生成特殊成员的简明概述,包括移动操作:
Declaration | Copy Constructor | Copy Assignment Operator | Move Constructor & Move Assignment Operator |
---|---|---|---|
None | Generated | Generated | Not Generated |
Copy Constructor | Not Generated | Generated | Not Generated |
Copy Assignment Operator | Not Generated | Not Generated | Not Generated |
Move Constructor | Not Generated | Not Generated | Not Generated |
Move Assignment Operator | Not Generated | Not Generated | Not Generated |
Destructor | Not Generated | Not Generated | Not Generated |
Copy Constructor & Copy Assignment Operator | Not Generated | Not Generated | Not Generated |
Copy Constructor & Move Constructor | Not Generated | Not Generated | Not Generated |
Copy Constructor & Move Assignment Operator | Not Generated | Not Generated | Not Generated |
Copy Assignment Operator & Move Constructor | Not Generated | Not Generated | Not Generated |
Copy Assignment Operator & Move Assignment Operator | Not Generated | Not Generated | Not Generated |
Move Constructor & Destructor | Not Generated | Not Generated | Not Generated |
Move Assignment Operator & Destructor | Not Generated | Not Generated | Not Generated |
Copy Constructor & Copy Assignment Operator & Move Constructor | Not Generated | Not Generated | Not Generated |
Copy Constructor & Copy Assignment Operator & Move Assignment Operator | Not Generated | Not Generated | Not Generated |
Copy Constructor & Move Constructor & Move Assignment Operator | Not Generated | Not Generated | Generated |
Copy Assignment Operator & Move Constructor & Move Assignment Operator | Not Generated | Not Generated | Generated |
通过识别这些规则,开发人员可以确保他们的类正确利用移动语义并实现最佳效率。
以上是C 11 中何时自动生成移动构造函数和移动赋值运算符?的详细内容。更多信息请关注PHP中文网其他相关文章!