是否有任何情况下,RValue参考(&&)的返回是有用的? [英] Is there any case where a return of a RValue Reference (&&) is useful?

查看:93
本文介绍了是否有任何情况下,RValue参考(&&)的返回是有用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当函数应返回RValue参考时,是否有原因?一种技术或诀窍,或成语或模式?

Is there a reason when a function should return a RValue Reference? A technique, or trick, or an idiom or pattern?

MyClass&& func( ... );

我知道一般返回引用,但有时我们这样做,不是我们( T& T :: operator = (T)只是一个惯用的例子)。但是 T&& func(...)?有没有什么一般的地方,我们会从中获益?

I am aware of the danger of returning references in general, but sometimes we do it anyway, don't we (T& T::operator=(T) is just one idiomatic example). But how about T&& func(...)? Is there any general place where we would gain from doing that? Probably different when one writes library or API code, compared to just client code?

推荐答案

有一些情况下,当适当的时候,但它们相对较少。当您想要允许客户端从数据成员移动时,会出现一个示例。例如:

There are a few occasions when it is appropriate, but they are relatively rare. The case comes up in one example when you want to allow the client to move from a data member. For example:

template <class Iter>
class move_iterator
{
private:
    Iter i_;
public:
    ...
    value_type&& operator*() const {return std::move(*i_);}
    ...
};

这篇关于是否有任何情况下,RValue参考(&amp;&amp;)的返回是有用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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