C ++从具有取消引用类型的类型的函数返回引用? [英] C++ returning reference from function with type of dereferenced type?

查看:115
本文介绍了C ++从具有取消引用类型的类型的函数返回引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的函数中,我想获取一个向量objects并返回该向量之一的副本.由于下面的代码可以正确编译,因此我假设迭代器copy_objects.back()被自动取消引用.但是我不确定. return语句中发生了什么?

In the following function, I want to take a vector objects and return a copy of one of the elements of that vector. Since the code below compiles correctly, I'm assuming that the iterator copy_objects.back() is automatically dereferenced. But I don't know for certain. What is happening in the return statement?

MyObject best(vector<MyObject>& objects) {
    vector<MyObject> copy_objects = objects;
    sort(copy_objects.begin(), copy_objects.end(), compare_MyObject_func);
    return copy_objects.back();
}

我知道还有其他方法可以完成当前的简单任务,但是我很好奇这个示例中发生的事情.

I know there are other ways to accomplish the simple task at hand, but I'm curious to know what's going on in this example.

后续问题...

使用上面的best函数的定义,我得到以下编译错误:

Using the definition of the best function above, I get the following compile error:

error: invalid initialization of non-const reference of type ‘std::vector<MyObject>&’ from an rvalue of type ‘std::vector<MyObject>’
     bestObject = best(myfunction(objects));
                                          ^

其中相关的类型声明为:

Where the relevant type declarations are:

MyObject bestObject;
vector<MyObject> objects;
vector<MyObject> myfunction(vector<MyObject>&);

我的直觉告诉我这个错误与上面的原始问题有关.但是,我不明白问题出在哪里.

My intuition tells me this error is related to the original question above. But, I don't understand what the problem is.

推荐答案

取消引用"通常是指将一元*运算符应用于指针或其他迭代器(如*p).该术语令人困惑,因为它与C ++中的引用类型无关,因此标准委员会正在改用执行间接".

"Dereference" usually refers to applying the unary * operator to a pointer or other iterator (like *p). This terminology is confusing because it doesn't have anything to do with reference types in C++, so the standards committee are moving to use "perform indirection" instead.

无论如何,copy_objects.back()根本不返回迭代器.它返回对对象的引用(实际引用类型).您可以看到这是因为std::vector<T>::back的返回类型是const_reference,这是const value_type&的类型定义,其中value_typeT.您永远不会取消引用"引用.如果要从参考复制,则只需将其视为普通对象即可.

Anyway, copy_objects.back() does not return an iterator at all. It returns a reference to the object (an actual reference type). You can see this because the return type for std::vector<T>::back is const_reference which is a typedef for const value_type&, where value_type is T. You don't ever "dereference" references. If you want to copy from a reference, you just need to treat it like a normal object.

int x = 0;
int& y = x;
int z = y;

在此示例中,z将是xy都引用的对象的副本.

In this example, z will be a copy of the object referred to by both x and y.

除此之外,我建议也只按值取参数(删除&):

In addition to this, I recommend just taking the argument by value too (remove the &):

MyObject best(vector<MyObject> objects) {
  sort(objects.begin(), objects.end(), compare_MyObject_func);
  return objects.back();
}

无论如何您都将复制它,因此通过引用来获取它是没有意义的.特别是非const引用建议您将修改给定的参数,而不必修改.实际上,当vector可能被移动到函数中时,参考参数会给您带来较差的性能.引用类型参数将永远不会移动.

You're going to copy it anyway, so taking it by reference is pointless. Especially a non-const reference suggests that you're going to modify the given argument, which you don't. In fact, a reference parameter gives you worse performance when the vector could be moved into the function. The reference type parameter will never move.

回答您的扩展问题:

myfunction按值返回,这意味着它返回的对象是一个临时对象(它是其中任何对象的副本).您不能将非const引用(best的参数)绑定到临时对象.这是有道理的,因为这样临时对象将消失(因为它是临时的),并且引用将保留为空.

myfunction returns by value, which means that the object it returns is a temporary object (it's a copy of whatever was inside it). You can't bind a non-const reference (the parameter of best) to a temporary object. This makes sense because then the temporary object would disappear (because it's temporary) and the reference would be left referring to nothing.

如果将参数类型设置为const引用,则可以,因为const引用会延长临时对象的寿命.如果将参数设置为非引用,也可以,因为它将创建临时对象的本地副本.

If you made the parameter type a const reference, it would be fine, because const references extend the lifetime of a temporary object. If you made the parameter a non-reference, it would also be fine, because it would make a local copy of the temporary object.

这篇关于C ++从具有取消引用类型的类型的函数返回引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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