通过调用移动分配运算符实现移动构造函数 [英] Implementing Move Constructor by Calling Move Assignment Operator

查看:107
本文介绍了通过调用移动分配运算符实现移动构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN文章如何:撰写移动导览,有以下建议。

The MSDN article, How to: Write a Move Constuctor, has the following recommendation.


如果您为类提供了移动构造函数和移动赋值运算符,则可以通过编写
移动构造函数来调用move赋值运算符。
下面的示例显示了
调用move赋值运算符的move构造函数的修订版本:

If you provide both a move constructor and a move assignment operator for your class, you can eliminate redundant code by writing the move constructor to call the move assignment operator. The following example shows a revised version of the move constructor that calls the move assignment operator:



// Move constructor.
MemoryBlock(MemoryBlock&& other)
   : _data(NULL)
   , _length(0)
{
   *this = std::move(other);
}

这个代码通过双重初始化 MemoryBlock 的值,还是编译器能够优化掉额外的初始化?我应该总是通过调用move赋值运算符写我的移动构造函数?

Is this code inefficient by doubly initializing MemoryBlock's values, or will the compiler be able to optimize away the extra initializations? Should I always write my move constructors by calling the move assignment operator?

推荐答案

我不会这样做。移动成员首先存在的原因是性能。这样做你的移动构造函数就像为超级跑车抛出巨额卡车,然后通过购买常规气体来节省资金。

I wouldn't do it this way. The reason for the move members to exist in the first place is performance. Doing this for your move constructor is like shelling out megabucks for a super-car and then trying to save money by buying regular gas.

如果你想减少代码你写的,只是不要写移动成员。

If you want to reduce the amount of code you write, just don't write the move members. Your class will copy just fine in a move context.

如果你想让你的代码具有高性能,那么你需要定制你的移动构造函数和移动赋值以尽可能快。好的移动成员会飞快的,你应该通过计算负载,商店和分支来估计他们的速度。如果你可以用4个加载/商店而不是8写一些东西,那就做吧!

If you want your code to be high performance, then tailor your move constructor and move assignment to be as fast as possible. Good move members will be blazingly fast, and you should be estimating their speed by counting loads, stores and branches. If you can write something with 4 load/stores instead of 8, do it! If you can write something with no branches instead of 1, do it!

当你(或你的客户)把你的类放到一个 std: :vector ,可以在您的类型上生成很多的移动。即使你的移动在8个装载/商店快速闪电,如果你可以使它的速度提高一倍,甚至50%的速度,只有4或6个装载/商店,imho这是时间花费。

When you (or your client) put your class into a std::vector, a lot of moves can get generated on your type. Even if your move is lightning fast at 8 loads/stores, if you can make it twice as fast, or even 50% faster with only 4 or 6 loads/stores, imho that is time well spent.

我个人对看到等待游标感到厌烦,愿意捐赠额外的5分钟来写我的代码,并知道它尽可能快。

Personally I'm sick of seeing waiting cursors and am willing to donate an extra 5 minutes to writing my code and know that it is as fast as possible.

如果你仍然不相信这是值得的,写两个方法,然后检查生成的程序集在全面优化。谁知道,你的编译器可能是聪明的,以优化离开额外的负载和存储为你。但是到这个时候,你已经投入了更多的时间,比如果你刚刚编写了一个优化的移动构造函数。

If you're still not convinced this is worth it, write it both ways and then examine the generated assembly at full optimization. Who knows, your compiler just might be smart enough to optimize away extra loads and stores for you. But by this time you've already invested more time than if you had just written an optimized move constructor in the first place.

这篇关于通过调用移动分配运算符实现移动构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆