基督教徒的破坏者是否可以阻止在子类中创建移动构造函数?

发布于 2025-01-26 02:30:24 字数 295 浏览 2 评论 0原文

按照这篇文章,它说[强调我的]:

如果没有用户指定的复制构造函数,复制分配运算符或 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话墙 2025-02-02 02:30:24

The compiler will generate the move constructors for you:

#include <iostream>
#include <memory>
#include <utility>

struct Foo {
    virtual ~Foo() {}
};

struct Bar : public Foo {
    Bar() : pi(new int(5)) {};

    std::unique_ptr<int> pi;
};

int main()
{
    Bar b;
    std::cout << b.pi.get() << std::endl;
    Bar c = std::move(b);
    std::cout << b.pi.get() << std::endl;
    std::cout << c.pi.get() << std::endl;
}

which (on a given run), output:

0x1b65e70
0
0x1b65e70

如果编译器没有生成移动构建器, c c c c 无法构造(因为使用std :: sunily_ptr作为成员,不允许使用隐式副本),显然,对指针的所有权已正确传输。该代码是在GCC下编译的,警告在-STD = C ++ 17模式中最大化,没有问题。

The compiler will generate the move constructors for you:

#include <iostream>
#include <memory>
#include <utility>

struct Foo {
    virtual ~Foo() {}
};

struct Bar : public Foo {
    Bar() : pi(new int(5)) {};

    std::unique_ptr<int> pi;
};

int main()
{
    Bar b;
    std::cout << b.pi.get() << std::endl;
    Bar c = std::move(b);
    std::cout << b.pi.get() << std::endl;
    std::cout << c.pi.get() << std::endl;
}

which (on a given run), output:

0x1b65e70
0
0x1b65e70

Try it online!

If the compiler had not generated the move constructor, c could not have been constructed (since by using std::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.

雪落纷纷 2025-02-02 02:30:24

您可以通过查看类定义来确定成员函数是否是用户宣布的。如果您看到该功能,则是用户遵守的。

例如:

class Derived : public Base {
   ~Derived() = default; // <-- see this? user-declared destructor
};

相反:

class Derived : public Base {
   // <-- see a destructor here? No. So not a user-declared destructor
};

在两种情况下,编译器都会生成相同的破坏者主体,但是前一个示例具有用户宣布的驱动器,而后者则具有自动生成的驱动器。在这种情况下,用户指定的驱动器是编译器生成的,但是通过用户的请求生成的,因此不是 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:

class Derived : public Base {
   ~Derived() = default; // <-- see this? user-declared destructor
};

In contrast to:

class Derived : public Base {
   // <-- see a destructor here? No. So not a user-declared destructor
};

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文