什么时候添加一个移动构造函数和一个移动赋值操作符真的开始有所作为? [英] When will adding a move constructor and a move assignment operator really start make a difference?

查看:189
本文介绍了什么时候添加一个移动构造函数和一个移动赋值操作符真的开始有所作为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到今天的编译器对于返回值优化(RVO和NRVO)的高质量,我想知道什么类复杂度开始添加移动构造函数和移动赋值运算符是有意义的。

Considering the high quality of today's compilers regarding return value optimization (both RVO and NRVO), I was wondering at what class complexity it's actually meaningful to start adding move constructors and move assignment operators.

例如,对于这个 really_trivial 类,我只是假定move语义不能提供任何更多的RVO和NRVO已经做的时候复制它的实例:

For instance, for this really_trivial class, I just assume that move semantics cannot offer anything more than RVO and NRVO already does when copying around instances of it:

class really_trivial
{
    int first_;
    int second_;

public:

    really_trivial();
    ...
};

semi_complex d添加一个移动构造函数和移动赋值运算符,毫不犹豫:

While in this semi_complex class, I'd add a move constructor and move assignment operator without hesitation:

class semi_complex
{
    std::vector<std::string> strings_;

public:

    semi_complex(semi_complex&& other);
    semi_complex& operator=(semi_complex&& other);
    ...
};

因此,在什么数量和什么类型的成员变量开始有意义添加move构造函数并移动赋值运算符?

So, at what amount and of what kinds of member variables does it start making sense to add move constructors and move assignment operators?

推荐答案

它纯粹是语义上的意义,即使你完全保持优化。只是想象一个类不能/不能实现复制的情况。例如 boost :: scoped_ptr 无法复制,但可以移动!

It is already meaningful for purely semantic reasons, even if you keep the optimization-aspects out completely. Just imagine the case where a class does not/cannot implement copying. For example boost::scoped_ptr cannot be copied, but it can be moved!

这篇关于什么时候添加一个移动构造函数和一个移动赋值操作符真的开始有所作为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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