应该在返回语句中使用std :: mo来提高效率吗? [英] Should std::move be used in return-statements for effeciency?

查看:54
本文介绍了应该在返回语句中使用std :: mo来提高效率吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚以下代码中的std :: move是否有任何好处或完全错误? 类Object同时定义了Move和Copy构造函数.

I cannot figure out if the std::move in the following code does anything good or that it is completely wrong? The class Object has both Move and Copy constructor defined.

首先:移动:

template<typename T> template <typename F> 
const Object<T> Object<T>::operator*(const F& rhs) const 
{
    return std::move(Object(*this) *= rhs);  // We end in move constructor
}

第二:不移动:

template<typename T> template <typename F> 
const Object<T> Object<T>::operator*(const F& rhs) const 
{
    return Object(*this) *= rhs; // We end in copy constructor
}

*=运算符定义为:

template<typename T> template<typename F>  
Object<T>& Object<T>::operator*=(const F& rhs) 
{
    for(int i = 0; i < dimension ; i++)
    {
        _inner[i] *= rhs;
    }
    return *this;
}

这是我用来测试的代码:

Here is the code i use to test it:

Object<double> test(4);
Object<double> test2(test * 4);
std::cout << test2; // works fine

结果 first 情况下,我们以move构造函数结尾,在 second 情况下,我们以拷贝构造函数结尾.

Result In the first case we end in the move constructor and in the second we end in the copy constructor.

无论哪种情况,代码都会编译.

In either case the code compiles.

一种方法是否比另一种方法更有效率,因为我认为将新对象移出而不是将其复制出来会更快?

其他信息: 我使用以下编译器:g ++(Ubuntu/Linaro 4.7.3-1ubuntu1)4.7.3

Additional info: I use the following compiler: g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3

推荐答案

一种方法是否比另一种方法更有效率,因为我认为将新对象移出而不是将其复制出来会更快?

Is one more efficient than the other since I would assume it is faster to move the new object out instead of copying it out?

是的,假设对象的移动语义比复制更有效,则在此处使用std::move会更高效.

Yes, using std::move here will be more efficient, assuming the object has move semantics more efficient than copying.

通常,当返回一个临时或局部变量时,将自动使用move语义.但是,在这种情况下,您不是直接返回临时目录,而是由operator*=返回的 lvalue 引用.由于左值不会移动,因此在这种情况下,您确实需要std::move才能将其转换为右值.

Usually, when returning a temporary or a local variable, move semantics will be used automatically. However, in this case you're not directly returning the temporary, but rather the lvalue reference returned by operator*=. Since an lvalue won't be moved, you do need std::move in this case to turn it into an rvalue.

但是,您不应该返回const值,因为这会阻止将返回值用于移动初始化(或移动分配给另一个对象).您的示例将通过复制返回值来初始化test2,尽管该副本可能会被删除.

However, you should not return a const value, as this prevents the return value from being used to move-initialise (or move-assign to) another object. Your example will initialise test2 by copying the return value, although that copy might be elided.

或者,您可以使用局部变量来实现它:

Alternatively, you could implement it using a local variable:

template<typename T> template <typename F> 
Object<T> Object<T>::operator*(const F& rhs) const 
{
    Object lhs(*this);
    lhs *= rhs;
    return lhs;
}

不仅可以移动返回值,而且可以忽略移动本身.

Not only can the return value be moved, but the move itself can be elided.

这篇关于应该在返回语句中使用std :: mo来提高效率吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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