在c ++ 11中使用右值引用 [英] Using of rvalue references in c++11

查看:348
本文介绍了在c ++ 11中使用右值引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个函数填充一个向量,然后返回一个右值引用。我累了像:

I would like to implement a function that fills up a vector and then returns an rvalue reference. I tired something like:

std::vector<int> &&fill_list() {
  std::vector<int> res;
  ... do something to fill res ...
  return res;
}

int main(int argc, char **argv) {
  std::vector<int> myvec = fill_list();
  return 0;
}

但不起作用,我收到以下错误:

but that doesn't work, I get the following error:

error: invalid initialization of reference of type 'std::vector<int>&&' from expression of type 'std::vector<int>'

所以,总而言之,正确的做法是什么?我不认为我还得到右值引用。

So, all in all, how is the right way of doing it? I don't think I get rvalue references just yet.

推荐答案

你似乎困惑于什么是右值引用,如何与移动语义相关。

You seem to be confused as to what an rvalue reference is and how it relates to move semantics.

第一件事:&&& 。它只是一种特殊的引用类型。仍然是 参考。这不是一个价值;它不是;它是一个参考值。这意味着它具有引用类型的所有限制。值得注意的是,它必须引用仍存在的值。所以返回一个悬挂的r值引用并不比返回一个悬空的l值引用要好。

First thing's first: && does not mean move. It is nothing more than a special reference type. It is still a reference. It is not a value; it is not a moved value; it is a reference to a value. Which means it has all of the limitations of a reference type. Notably, it must refer to a value that still exists. So returning a dangling r-value reference is no better than returning a dangling l-value reference.

移动是使一个对象声明对内容的所有权的过程的另一个对象。 R值引用便于移动语义,但只需要一个&& 并不意味着任何内容已移动。仅当调用移动构造函数(或移动赋值运算符)时才发生移动

"Moving" is the process of having one object claim ownership of the contents of another object. R-value references facilitate move semantics, but simply having a && does not mean anything has moved. Movement only happens when a move constructor (or move assignment operator) is called; unless one of those two things is called, no movement has occurred.

如果你想移动 std :: vector

If you wish to move the contents of a std::vector out of your function to the user, you simply do this:

std::vector<int> fill_list() {
  std::vector<int> res;
  ... do something to fill res ...
  return res;
}

鉴于此用法 fill_list c $ c>:

Given this usage of fill_list():

std::vector<int> myvec = fill_list();

两种情况之一会发生。或者返回将被省略,这意味着不复制移动发生。 res 直接构建到 myvec 中。或 res 将移动到返回值,然后将执行 myvec 的移动初始化。

One of two things will happen. Either the return will be elided, which means that no copying or moving happens. res is constructed directly into myvec. Or res will be moved into the return value, which will then perform move-initialization of myvec. So again, no copying.

如果你有这样的:

std::vector<int> myvec;
myvec = fill_list();

然后再次移动到。无复制。

Then again, it would be moved into. No copying.

C ++ 11知道什么时候安全地隐式移动东西。通过值而不是引用返回值总是一个安全的移动时间。因此,它会移动。

C++11 knows when it's safe to implicitly move things. Returning a value by value rather than by reference or something is always a safe time to move. Therefore, it will move.

这篇关于在c ++ 11中使用右值引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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