在什么情况下,我应该明确需要实现一个移动构造函数和移动赋值运算符? [英] In what scenarios should I expect to explicitly need to implement a move constructor and move assignment operator?

查看:126
本文介绍了在什么情况下,我应该明确需要实现一个移动构造函数和移动赋值运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个类实际上是是可移动的,手动实现一个类的移动构造函数和移动赋值操作符很快就变得乏味。





例如,如果一个类只有一个简单的POD数据或成员它们自己有移动构造函数和移动赋值运算符定义,然后我猜测编译器将只是优化shit出了很多(在POD的情况下),否则使用成员的移动构造函数和移动赋值运算符。 / p>

但这是保证吗?在我应该期望应该明确需要实现移动构造函数和移动赋值运算符的情况下。



EDIT:如下面Nicol Bolas在 http://stackoverflow.com/a/9966105/6345 的回答中所述,自动生成移动构造函数或移动分配运算符 。参考: http://blogs.msdn.com/b/vcblog/archive/ 2011/09/12 / 10209291.aspx

解决方案

如果您发现自己正在实施, >


  • 复制

  • 复制构造函数




然后你应该问自己,如果你需要实现移动构建。如果你=默认任何上述,你应该问自己,如果你应该然后也=默认移动成员。



更重要的是,你应该记录和测试您的假设,例如:

  static_assert(std :: is_nothrow_default_constructible< A> :: value, ; 
static_assert(std :: is_copy_constructible< A> :: value,);
static_assert(std :: is_copy_assignable< A> :: value,);
static_assert(std :: is_nothrow_move_constructible< A> :: value,);
static_assert(std :: is_nothrow_move_assignable< A> :: value,);
static_assert(std :: is_nothrow_destructible< A> :: value,);


Given that a class actually is moveable, manually implementing the move constructor and move assignment operator for a class quickly become tedious.

I was wondering when doing so is actually a heavy, heavy, premature optimization?

For instance, if a class only has trivial POD data or members that themselves have move constructor and move assignment operator defined, then I'd guess that the compiler will either just optimize the shit out of the lot (in the case of PODs) and otherwise use the members' move constructor and move assignment operator.

But is that guaranteed? In what scenarios should I expect to explicitly need to implement a move constructor and move assignment operator?

EDIT: As mentioned below by Nicol Bolas in a comment to his answer at http://stackoverflow.com/a/9966105/6345, with Visual Studio 11 Beta (and before) no move constructor or move assignment operator is ever automatically generated. Reference: http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx

解决方案

If you find yourself implementing, any of:

  • destructor
  • copy constructor
  • copy assignment

Then you should be asking yourself if you need to implement move construction. If you "= default" any of the above, you should be asking yourself if you should then also "= default" the move members.

Even more importantly, you should be documenting and testing your assumptions, for example:

static_assert(std::is_nothrow_default_constructible<A>::value, "");
static_assert(std::is_copy_constructible<A>::value, "");
static_assert(std::is_copy_assignable<A>::value, "");
static_assert(std::is_nothrow_move_constructible<A>::value, "");
static_assert(std::is_nothrow_move_assignable<A>::value, "");
static_assert(std::is_nothrow_destructible<A>::value, "");

这篇关于在什么情况下,我应该明确需要实现一个移动构造函数和移动赋值运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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