从r值ref限定方法返回r值参考是一种好习惯吗? [英] Is it a good practice to return the r-value reference from the r-value ref-qualified method?

查看:139
本文介绍了从r值ref限定方法返回r值参考是一种好习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如我所见,一般规则是完全不从函数返回r值引用(罕见的除外)特别案例)。但是类方法呢?

As I can see the general rule is not to return r-value references from functions at all (except for rare special cases). But what about class methods?

C ++标准库中有一个示例,该示例从类的r值ref限定的方法返回r值引用(< a href = http://en.cppreference.com/w/cpp/utility/optional/operator* rel = nofollow noreferrer> std :: optional< T> :: operator *( ) std :: optional< T> 类的> std :: optional< T> :: value() 方法)。请参见C ++ 17标准的 23.6.3类模板optional [optional.optional] 23.6.3.5 Observers [optional.observe] 部分:

There is an example in the C++ standard library of returning r-value reference from the r-value ref-qualified method of the class (std::optional<T>::operator*() and std::optional<T>::value() methods of the std::optional<T> class). See sections 23.6.3 Class template optional [optional.optional] and 23.6.3.5 Observers [optional.observe] of the C++17 standard:


// 23.6.3.5, observers

constexpr T&& operator*() &&;
constexpr const T&& operator*() const&&;
constexpr T&& value() &&;
constexpr const T&& value() const&&;



推荐答案

考虑类成员一般访问。假设我们有一个这样的类型:

Consider class member access in general. Say we have a type like this:

struct A {
  int x;
};

现在我们来看一个 A 类型的对象。

Now let's take an object of type A.

A a;

现在表达式(a)是一个左值,并且成员访问(a).x 也是左值。但是表达式 std :: move(a)是一个右值,而 std :: move(a).x 现在也是一个右值(实际上是一个xvalue)。这是核心语言中成员访问的行为。

Now the expression (a) is an lvalue, and member access (a).x is also an lvalue. But the expression std::move(a) is an rvalue, and std::move(a).x is now also an rvalue (in fact, an xvalue). This is the behaviour of member access in the core language.

现在,用户定义类型提供模仿核心语言行为的用户定义行为是有意义的。我们可以通过使用ref限定成员函数来做到这一点,成员函数可以区分实例是左值还是右值。如果成员函数的目的是提供对子对象的访问,则当实例为右值时,合理的是将该子对象作为右值(特别是xvalue)返回。

Now it makes sense for user-defined types to provide user-defined behaviour that mimics the core language behaviour. We can do this by using ref-qualified member functions, which can distinguish whether the instance is an lvalue or an rvalue. When the purpose of the member function is to provide access to a subobject, then it is reasonable to return that subobject as an rvalue (specifically, an xvalue) when the instance is an rvalue.

您可能要参考的一般建议是,您不应随意将右值引用返回给您无法控制的任意对象。在这种情况下,通常最好返回副本,这样就可以独立于无关对象的生存期的上下文假设来使用函数。但是,当您谈论成员函数并且所讨论的对象是类实例时,您具有更多的控制权,并且返回xvalues可能是有用的工具。

The general advice you're presumably referring to is that you shouldn't randomly be returning rvalue references to some arbitrary objects that you don't control. In that case, it is often much better to return copies, so that your function can be used independent of contextual assumptions on lifetimes of unrelated objects. But when you're talking about member functions and the object in question is the class instance, you have a bit more control, and returning xvalues can be useful tool.

A此处的元课程是,在C ++中询问特定的语法X是否是好的做法并不总是有用的。 C ++为您提供了许多工具,有时您不得不谈论上下文中的设计。

A meta lesson here is that it's not always useful to ask "whether <specific syntax X> is good practice" in C++. C++ gives you a lot of tools, and sometimes you have to talk about design in context.

这篇关于从r值ref限定方法返回r值参考是一种好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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