避免在移动构造函数中移动成员样板?
有时您仍然需要默认的移动构造函数行为(成员方式移动),但也希望修改移出的对象。以下面的场景为例,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论