简体   繁体   English

C++ 底漆 5 版。 虚拟析构函数和移动语义

[英]C++ primer 5 ed. Virtual destructor and move semantics

In C++ primer 5 ed.在 C++ 引物 5 版中。 it is said:据说:

"The fact that a base class needs a virtual destructor has an important indirect impact on the definition of base and derived classes: If a class defines a destructor—even if it uses = default to use the synthesized version—the compiler will not synthesize a move operator for that class (§13.6.2, p. 537)." “基类 class 需要虚拟析构函数这一事实对基类和派生类的定义具有重要的间接影响:如果 class 定义析构函数——即使它使用 = default 来使用合成版本——编译器也不会合成移动该 class 的运算符(第 13.6.2 节,第 537 页)。”

So for more understanding I've tried this:所以为了更多的理解,我试过这个:

class A
{
public:
    A(){cout << "A::A()\n";}
    virtual ~A(){cout << "A::~A()\n";}
    A& operator = (const A&) {cout << "A::=\n"; return *this;};
    A& operator = (A&&) = default;
};

int main()
{

    A a;
    A a2;
    a2 = std::move(a); // why copy-assignment operator is not called here. Normally a will be "moved" through copy-ctor (moving through working copy-ctor)


    std::cout << std::endl;
}
  • So according to the text above in the book class A has a virtual destructor thus the compiler won't synthesize a move-ctor/assignment operations But in main I've tried to move an object of type class A so normally move here uses copy-assignment instead of move-assignment but copy-assignment is not called here?!因此,根据class A有一个virtual析构函数,因此编译器不会合成一个 move-ctor/assignment 操作但main是我试图move一个 object 类型的class A -assignment而不是move-assignment,但这里不调用copy-assignment?!

  • So when a vairtual dtor causes a default operation be not synthesized?那么当虚拟 dtor 导致默认操作无法合成时呢? Thank you!谢谢!

Your textbook is right, the compiler won't automatically synthesize a move operator.您的教科书是对的,编译器不会自动合成移动运算符。 In the code sample you force the compiler to generate one by setting it to default, this can only fail if the move operator is implicitly deleted .在代码示例中,您通过将其设置为默认值来强制编译器生成一个,这只会在 move 运算符被隐式删除时失败。 Which is something else than it being implicitly declared .这不是隐式声明的。

Criteria for implicit deletion :隐式删除的标准:

  • A has a non-static data member that is const A 有一个非静态数据成员,它是 const
  • A has a non-static data member of a reference type A 具有引用类型的非静态数据成员
  • A has a non-static data member that cannot be move-assigned (has deleted, inaccessible, or ambiguous move assignment operator) A 具有无法移动分配的非静态数据成员(已删除、无法访问或不明确的移动分配运算符)
  • A has direct or virtual base class that cannot be move-assigned (has deleted, inaccessible, or ambiguous move assignment operator) A 具有无法移动分配的直接或虚拟基 class(已删除、无法访问或不明确的移动分配运算符)
  • A has a non-static data member or a direct or virtual base without a move assignment operator that is not trivially copyable A 有一个非静态数据成员或一个直接或虚拟基数,但没有移动赋值运算符,这不是一般可复制的
  • A has a direct or indirect virtual base class A有一个直接或间接的虚基class

Criteria for implicit declaration :隐式声明的标准:

  • there are no user-declared copy constructors;没有用户声明的复制构造函数;
  • 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隐式声明的移动赋值运算符不会被定义为已删除

source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM