避免在移动构造函数中移动成员样板?

发布于 2025-01-11 11:27:55 字数 839 浏览 0 评论 0原文

有时您仍然需要默认的移动构造函数行为(成员方式移动),但也希望修改移出的对象。以下面的场景为例,

class Base{...}; // some move-constructible class
class Member{...}; // also move constructible

class Derived : public Base
{
public:
    // does member-wise move, but nothing more
    // Derived(Derived &&) = default;

    // does member-wise move, same as default
    // and also modifies `other`
    Derived(Derived && other)
        : a(std::move(other.a))
        , b(std::move(other.b))
        , ... // the rest of members
    {
        other.put_into_invalid_state_or_something();
    }

    //...............................................
    //      More functions
    //...............................................

private:
    // a lot of members...
    Member a,b,c,d,e,f,g,h,i;
};

这可能是一个愚蠢的问题,但是有没有办法避免为每个成员都编写动作呢?

Sometimes you still want the default move-constructor behavior (member-wise move), but also want to modify the moved-from object. Take the following scenario, for example

class Base{...}; // some move-constructible class
class Member{...}; // also move constructible

class Derived : public Base
{
public:
    // does member-wise move, but nothing more
    // Derived(Derived &&) = default;

    // does member-wise move, same as default
    // and also modifies `other`
    Derived(Derived && other)
        : a(std::move(other.a))
        , b(std::move(other.b))
        , ... // the rest of members
    {
        other.put_into_invalid_state_or_something();
    }

    //...............................................
    //      More functions
    //...............................................

private:
    // a lot of members...
    Member a,b,c,d,e,f,g,h,i;
};

This may be a silly question, but is there any way to avoid writing the move for each member?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文