基督教徒的破坏者是否可以阻止在子类中创建移动构造函数?
按照这篇文章,它说[强调我的]:
如果没有用户指定的复制构造函数,复制分配运算符或 destructor ,并且如果生成的移动分配运算符是有效的,则将自动生成。需要分配常数成员)(§12.8/21)。
继承的驱动器计数吗?我的意思是,说我有一个带有空的虚拟驱动器的基础课。它会阻止在子类中创建移动构造函数吗?
As per this post, which says that[emphasise mine]:
The move assignment operator is auto-generated if there is no user-declared copy constructor, copy assignment operator or destructor, and if the generated move assignment operator is valid (e.g. if it wouldn't need to assign constant members) (§12.8/21).
Does an inherited destructor count? I mean, say I've got a base class with an empty virtual destructor. Does it prevent creation of move constructors in subclasses?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
The compiler will generate the move constructors for you:
which (on a given run), output:
如果编译器没有生成移动构建器, c c c c 无法构造(因为使用
std :: sunily_ptr
作为成员,不允许使用隐式副本),显然,对指针的所有权已正确传输。该代码是在GCC下编译的,警告在-STD = C ++ 17
模式中最大化,没有问题。The compiler will generate the move constructors for you:
which (on a given run), output:
Try it online!
If the compiler had not generated the move constructor,
c
could not have been constructed (since by usingstd::unique_ptr
as a member, implicit copy is not allowed), and clearly the ownership over the pointer was transferred properly. The code was compiled under GCC with warnings maxed out in-std=c++17
mode, with no issues.您可以通过查看类定义来确定成员函数是否是用户宣布的。如果您看到该功能,则是用户遵守的。
例如:
相反:
在两种情况下,编译器都会生成相同的破坏者主体,但是前一个示例具有用户宣布的驱动器,而后者则具有自动生成的驱动器。在这种情况下,用户指定的驱动器是编译器生成的,但是通过用户的请求生成的,因此不是 auto 生成的。
对于“用户宣布”,基础类别的情况并不重要。如果您看到列出的破坏者(如第一个示例),那么编译器将不会自动产生移动分配或移动构造。如果您看不到列出的损坏器,如第二个示例,那么需要检查自动生成的其他标准。
基类对自动生成的成员的影响仅限于“有效”标准。
You can tell if a member function is user-declared by looking at the class definition. If you see the function, then it is user-declared.
For example:
In contrast to:
The compiler generates the same destructor body in both cases, but the former example has a user-declared destructor, while the latter has an auto-generated destructor. The user-declared destructor is compiler-generated in this case, but it is generated at the user's request, so it is not auto-generated.
For "user-declared", it does not matter what's going on in the base class. If you see the destructor listed, as in the first example, then the compiler will not auto-generate move assignment or move construction. If you do not see the destructor listed, as in the second example, then the other criteria for auto-generation need to be checked.
The base class' influence on which members are auto-generated is restricted to the "is valid" criterion.