窃取移动构造函数内部
在玩具类的移动构造函数的实现过程中,我注意到一个模式:
array2D(array2D&& that)
{
data_ = that.data_;
that.data_ = 0;
height_ = that.height_;
that.height_ = 0;
width_ = that.width_;
that.width_ = 0;
size_ = that.size_;
that.size_ = 0;
}
该模式显然是:
member = that.member;
that.member = 0;
因此我编写了一个预处理器宏来使窃取变得不那么冗长和容易出错:
#define STEAL(member) member = that.member; that.member = 0;
现在的实现如下所示:
array2D(array2D&& that)
{
STEAL(data_);
STEAL(height_);
STEAL(width_);
STEAL(size_);
}
是否有任何缺点这?是否有一种不需要预处理器的更清洁的解决方案?
During the implementation of the move constructor of a toy class, I noticed a pattern:
array2D(array2D&& that)
{
data_ = that.data_;
that.data_ = 0;
height_ = that.height_;
that.height_ = 0;
width_ = that.width_;
that.width_ = 0;
size_ = that.size_;
that.size_ = 0;
}
The pattern obviously being:
member = that.member;
that.member = 0;
So I wrote a preprocessor macro to make stealing less verbose and error-prone:
#define STEAL(member) member = that.member; that.member = 0;
Now the implementation looks as following:
array2D(array2D&& that)
{
STEAL(data_);
STEAL(height_);
STEAL(width_);
STEAL(size_);
}
Are there any downsides to this? Is there a cleaner solution that does not require the preprocessor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是推荐的模式:
当然,如果数据成员是标量类型,则不需要
std::move
。但是,如果您要复制此模式,无论如何包含move
都会很有帮助,这样当成员数据不是标量时,std::move
就不会'不要被遗忘。此外,如果成员数据具有实际的移动构造函数,那么您可以简单地省略主体:
如果您想推广到没有移动构造函数但确实具有无资源默认构造状态的类型,您可以:
我建议这些语句的排序顺序与它们在 array2D 类定义中声明为数据成员的顺序相同。我发现主体中初始化程序列表的重复没有任何问题。这是必要的第二步。没有必要把它隐藏起来。
Here is the recommended pattern:
Naturally if the data members are scalar types, the
std::move
isn't needed. But if you're copying this pattern around, it is helpful to include themove
anyway so that when the member data aren't scalar, thestd::move
doesn't get forgotten.Also if the member data have actual move constructors, then you can simply omit the body:
And if you want to generalize to types that don't have move constructors, but do have a resource-less default constructed state, you can:
I recommend ordering these statements in the same order they are declared as data members in the
array2D
class definition. And I find nothing wrong with the repetition of the initializer list in the body. It is a necessary and second step. There is no need to sweep it under the rug.使用
template
怎么样:用法:
@Fred,根据您的评论,如果您想避免两次提及数据成员,则:
用法:
How about using
template
:Usage:
@Fred, from your comment, if you want to avoid mentioning data member twice, then:
Usage:
将您自己的成员初始化为默认值,然后
交换
。甚至更干净(如果你的编译器支持它)
Initialize your own members to default, and then
swap
.Even cleaner (if your compiler supports it)